I'm trying to learn Django by implementing a bicycle serial number
database. A bike is a specific model from a given manufacturer. Each
frame of a given model can have multiple serial numbers at different
locations on the frame. These relationships are easily captured in this
simplified model definition:
class Manufacturer(models.Model):
name = models.CharField(max_length=64)
class Model(models.Model):
manufacturer = models.ForeignKey(Manufacturer)
name = models.CharField(max_length=64)
class Frame(models.Model):
model = models.ForeignKey(Model)
year = models.IntegerField(blank=True, null=True)
class SerialNumber(models.Model):
number = models.CharField(max_length=64)
location = models.CharField(max_length=4,
choices=LOCATION_CHOICES) #
LOCATION_CHOICES is elided here...
frame = models.ForeignKey(Frame)
Now, this application is essentially a means to populate a database, so
I've been tempted to just use the admin interface and dole out logins for
people I trust. That has two problems. One, I will eventually encounter
someone who's not trustworthy. Two, I won't learn much about creating
forms.
So, I'd like to create a form which allows users to add a frame to the
database with the opportunity to submit multiple serial numbers at one go.
It's not clear to me what I should use. I sort of think inline formsets
are candidates, but have had precious little luck finding an explanation of
what their object is, and even less luck finding a tutorial type example.
I did find an example here:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets
which looked promising, and I was able to adapt the BookFormSet and print
its as_table() output (lots of interesting HTML), but I can't make it work
in "real life". I defined these SNFormSet and AddForm classes:
SNFormSet = inlineformset_factory(Frame, SerialNumber)
class AddForm(forms.Form):
man =
forms.ModelChoiceField(queryset=Manufacturer.objects.order_by("name"),
label="Manfacturer")
mod = forms.CharField(max_length=100, label="Model")
snset = SNFormSet(instance=Frame())
When I display the form, I see widgets for the manufacturer and model, but
nothing related to the SNFormSet. I must be missing some connection between
the SNFormSet instance and the AddForm, but It's not clear what.
Any suggestions?
Thx,
Skip Montanaro
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.