0

这肯定与这个问题不同。

在我的问题中,问题是它为一个甚至不存在的字段显示 ValueError 这是我正在执行批量插入的模型

class Seats(models.Model):
    seat_no = models.ManyToManyField(Snumber)
    movie_name = models.ForeignKey(Movies)
    multiplex_name = models.ForeignKey(Multiplex)
    date = models.ForeignKey(Date)
    time = models.ForeignKey(Time)

    def __str__(self):
        b = str(self.date)
        c = str(self.time)
        d = str(self.multiplex_name)
        return d+" "+b+" "+c

    class Meta:
        unique_together = ('movie_name', 'multiplex_name', 'date', 'time')
        verbose_name_plural="Seats" 

我在 mov 中有一个 Movies 对象,在 mul 中有 Multiplex 并且在 dt 中有多个 Date 对象,在 tm 中有 Time 和在 st 中有 Snumber

我想在 dt 中的每个日期在 tm 中的每个时间添加所有 st 对象,在 mul 中使用多路复用,在 mov 中添加电影

这是我失败的尝试

Seats.objects.bulk_create([
        Seats(movie_name = mov,
                multiplex_name = mul,
                       seat_no = set, 
                         date = dat, 
                         time = tim
             )for dat in dt for tim in tm for set in st])

它显示 ValueError

    Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "<console>", line 2, in <listcomp>
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 550, in __init__
    setattr(self, prop, kwargs[prop])
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 499, in __set__
    manager = self.__get__(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 476, in __get__
    return self.related_manager_cls(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/related_descriptors.py", line 783, in __init__
    (instance, self.source_field_name))
ValueError: "<Seats: Badrinath Ki Dulhania 2017-05-07 09:00:00>" needs to have a value for field "seats" before this many-to-many relationship can be used.

它告诉我为现场席位设置一个值,但我的任何模型中都没有任何名为席位的字段。我错过了什么帮助!

4

0 回答 0