问题
I have a very simple model for tracking events:
class Event(models.Model):
description = models.TextField()
location = models.ForeignKey(Location)
start = models.TimeField()
duration = models.IntegerField()
event_date = models.DateField()
creator = models.ForeignKey(User)
and I opted to extract the location in a separate table in order to be able to perform queries:
class Location(models.Model):
city = models.CharField(max_length=20)
address = models.CharField(max_length=30).
What would be the best way to create a form for inserting/updating events, taking into account that the location should be done separately and than linked via ID in the event instance? I wanted to da maybe some autocomplete field for the location address and city, but to keep it on the same form for simplicity?
回答1:
Define your own widget for the ModelChoiceField
in the ModelForm
that is generated for the ForeignKey
.
来源:https://stackoverflow.com/questions/6576375/django-one-form-two-models