Django (and database) newbie here.
I am trying to puzzle out a method to enable the development of n custom text fields for any table (let us refer to it as Book) while using admin interface. I'd like a means for that user to define new text fields with the admin interface (rather than determining fields like CustomField1, CustomField2, etc, via a model then running manage.py syncdb).
Ultimately, I may wish to produce a separate table known as CustomFields. The django admin user (who isn't a programmer), would go and go into the custom text fields within this table (e.g. pubdate, isbn, country). Then, when you are performing data entry for any Book, they'd hit "+" for each custom area they wanted, ask them to obtainable in a dropdown, and add associated text for that custom area. The written text joined for every area is specific towards the parent Book.
Any suggestions? I've got a feeling there is a simple solution that I am in some way not grasping here.
In which you might encounter problems happens because Django won't recreate tables or posts according to altering model terms. What this means is you are not able to include fields to some table at run-time without running the produced sql in your database by hand. What you will need, rather, is really a method of determining custom fields, and connecting these to your model.
What I'd suggest may be the following:
class CustomField(models.Model): name = models.CharField(max_length=32) class Book(models.Model): ... fields class CustomValue(models.Model): field = models.ForeignKey(CustomField) value = models.CharField(max_length=255) book = models.ForeignKey(Book)
The validation around the above is rather non-existant, which does not permit you to define needed custom fields for every model, however, you should have the ability to develop that part if you want it afterwards.
# taken and modified from django online tutorial class CustomValueInline(admin.StackedInline): model = CustomValue extra = 3 class BookAdmin(admin.ModelAdmin): fieldsets = [ # your fields in here ] inlines = [CustomValueInline] admin.site.register(Book, BookAdmin)
This can allow customers to choose as much as 3 custom fields and values directly, using the choice to increase the when they wish.
Edit: Transformed the response to reflect more information in comments.
For that beginning you may create one model for that book and something for that text area, both connected via a foreign key relation. It is simple to administrate this then through django's inline admins, which will allow you to include more text fields!
# models.py from django.db import models class Book(models.Model): title = models.CharField(max_length=100) class TextField(models.Model): text = models.TextField book = models.ForeignKey(Book) # admin.py from django.contrib import admin from models import TextField, Book class TextAdmin(admin.TabularInline): model = TextField class BookAdmin(admin.ModelAdmin): inlines = [TextAdmin] admin.site.register(Book, BookAdmin)