我有模特
class Event(models.Model):
some_field = models.CharField()
class Ticket(models.Model):
event = models.ForeignKey(Event)
我如何设置模型票证字段事件,带有some_field字段的事件? 示例:
Ticket.objects.create(event__some_field = 'value')
您如何实现这一目标?
ps。我无法在我的情况下使用ticket.event_id = event_id和ticket.event =事件(我不想向数据库请求)
答案 0 :(得分:1)
您必须与创建故障单分开创建和保存事件实例。
event = Event(some_field='value')
event.save()
ticket = Ticket()
ticket.event = event
ticket.save()
如果您想更新与故障单相关的现有事件:
ticket.event.some_field = "new value"
ticket.event.save()
# no need to save ticket here, because nothing has changed in the relationship between ticket and event