Django one form / two models

北城以北 提交于 2019-12-11 02:13:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!