Django:添加多对多字段

时间:2018-06-02 05:09:38

标签: django

我目前正在努力为我新创建的对象分配多对多字段。谁知道我做错了什么?

注意:我有一个解决方案,首先创建故障单,然后尝试分配它。可能是这样做的吗?

    #Create ticket_tax
    assign_event = Event.objects.all()
    for event in assign_event:
        TicketTax.objects.create(
            event=event,
            name=lorem,
            percentage=0.19,
        )

    # Create tickets
    price_gross = ['40.60', '30.30', '100.40', ]
    name = ['Early Bird', 'Regular Ticket', 'Last Minute Ticket', ]
    assign_event = Event.objects.all().first()
    assign_ticket_tax = TicketTax.objects.all().first()

    for i in range(len(price_gross)):
        Ticket.objects.create(
            event=assign_event,
            ticket_tax=assign_ticket_tax.add(),
            price_gross=price_gross[i],
            name=name[i],
            description='ABC',
            start_at='2018-05-26 18:12:58.556925+02',
            end_at='2018-05-29 18:12:58.556925+02',
            quantity=100,
            status='On sale',
        )

models.py

class TicketTax(models.Model):
    event = models.ForeignKey(
        Event,
        on_delete=models.PROTECT,
        related_name='ticket_taxes'
    )
    name = models.CharField(max_length=100)
    percentage = models.DecimalField(
        max_digits=5,
        decimal_places=4
    )
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


class Ticket(models.Model):
    event = models.ForeignKey(
        Event,
        on_delete=models.PROTECT,
        related_name='tickets'
    )
    ticket_tax = models.ManyToManyField(TicketTax, blank=True)
    price_gross = models.DecimalField(
        max_digits=25,
        decimal_places=2
    )
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True, null=True)
    start_at = models.DateTimeField()
    end_at = models.DateTimeField()
    quantity = models.PositiveIntegerField()
    status = models.CharField(
        max_length=8,
        choices=TicketStatus.CHOICES
    )
    is_archived = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

1 个答案:

答案 0 :(得分:1)

  

注意:我有一个解决方案,首先创建故障单,然后尝试分配它。可能是这样做的吗?

这是完全正确的。您无法实例化对象并在一个create语句中分配M2M - 因为两个对象都需要存在于数据库中。 for循环内部应该(大致)执行上面的代码尝试执行的操作:

ticket = Ticket.objects.create(
        event=assign_event,
        price_gross=price_gross[i],
        name=name[i],
        description='ABC',
        start_at='2018-05-26 18:12:58.556925+02',
        end_at='2018-05-29 18:12:58.556925+02',
        quantity=100,
        status='On sale',
    )
ticket.ticket_tax.add(TicketTax.objects.all().first())