开发者

Django one form / two models

开发者 https://www.devze.com 2023-03-17 18:41 出处:网络
I have a very simple model for tracking events: class Event(models.Model): description = models.TextField()

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 =开发者_StackOverflow社区 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?


Define your own widget for the ModelChoiceField in the ModelForm that is generated for the ForeignKey.

0

精彩评论

暂无评论...
验证码 换一张
取 消