IntegrityError with DateField - 使用 get_or_create 创建对象时
IntegrityError with DateField - when creating objects with get_or_create
我在使用 Django get_or_create 时遇到问题,每当我创建具有相同日期的相同对象时,都会弹出完整性错误。
我的模型中有如下字段。
class Cart(models.Model):
created = models.DateTimeField(
pgettext_lazy('Cart field', 'created'), auto_now_add=True)
last_status_change = models.DateTimeField(
pgettext_lazy('Cart field', 'last status change'), auto_now_add=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL, blank=True, null=True, related_name='carts',
verbose_name=pgettext_lazy('Cart field', 'user'))
email = models.EmailField(
pgettext_lazy('Cart field', 'email'), blank=True, null=True)
def add(self,hoarding, date_from, date_until):
cart_line, created = self.lines.get_or_create(
hoarding=hoarding,date_from=date_from,date_until=date_until)
class Meta:
ordering = ('-last_status_change',)
verbose_name = pgettext_lazy('Cart model', 'Cart')
verbose_name_plural = pgettext_lazy('Cart model', 'Carts')
def __str__(self):
return smart_str(self.user)
class CartLine(models.Model):
cart = models.ForeignKey(
Cart, related_name='lines',
verbose_name=pgettext_lazy('Cart line field', 'cart'))
hoarding = models.ForeignKey(
Hoarding, related_name='+',blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'hoarding'))
date_from = models.DateField(blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'from'))
date_until = models.DateField(blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'until'))
class Meta:
unique_together = ('cart', 'date_from', 'date_until')
verbose_name = pgettext_lazy('Cart line model', 'Cart line')
verbose_name_plural = pgettext_lazy('Cart line model', 'Cart lines')
当我尝试将具有相同开始日期和结束日期的相同对象添加到购物车时出现错误:
IntegrityError at /hoardings/hoardings-demo-2-5/add/
UNIQUE constraint failed: cart_cartline.cart_id, cart_cartline.date_from, cart_cartline.date_until
get_or_create
在创建具有相同 dates.I 的对象时会 return IntegrityError 在日期字段中添加独特的功能但得到相同的错误。我使用 Django 1.11 和 Python 2.7
我重置了几次数据库,但没有用,数据库是 Postgres/sqlite。
unique_together
约束导致错误。去掉一样就好了
我在使用 Django get_or_create 时遇到问题,每当我创建具有相同日期的相同对象时,都会弹出完整性错误。
我的模型中有如下字段。
class Cart(models.Model):
created = models.DateTimeField(
pgettext_lazy('Cart field', 'created'), auto_now_add=True)
last_status_change = models.DateTimeField(
pgettext_lazy('Cart field', 'last status change'), auto_now_add=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL, blank=True, null=True, related_name='carts',
verbose_name=pgettext_lazy('Cart field', 'user'))
email = models.EmailField(
pgettext_lazy('Cart field', 'email'), blank=True, null=True)
def add(self,hoarding, date_from, date_until):
cart_line, created = self.lines.get_or_create(
hoarding=hoarding,date_from=date_from,date_until=date_until)
class Meta:
ordering = ('-last_status_change',)
verbose_name = pgettext_lazy('Cart model', 'Cart')
verbose_name_plural = pgettext_lazy('Cart model', 'Carts')
def __str__(self):
return smart_str(self.user)
class CartLine(models.Model):
cart = models.ForeignKey(
Cart, related_name='lines',
verbose_name=pgettext_lazy('Cart line field', 'cart'))
hoarding = models.ForeignKey(
Hoarding, related_name='+',blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'hoarding'))
date_from = models.DateField(blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'from'))
date_until = models.DateField(blank=True, null=True,
verbose_name=pgettext_lazy('Cart line field', 'until'))
class Meta:
unique_together = ('cart', 'date_from', 'date_until')
verbose_name = pgettext_lazy('Cart line model', 'Cart line')
verbose_name_plural = pgettext_lazy('Cart line model', 'Cart lines')
当我尝试将具有相同开始日期和结束日期的相同对象添加到购物车时出现错误:
IntegrityError at /hoardings/hoardings-demo-2-5/add/
UNIQUE constraint failed: cart_cartline.cart_id, cart_cartline.date_from, cart_cartline.date_until
get_or_create
在创建具有相同 dates.I 的对象时会 return IntegrityError 在日期字段中添加独特的功能但得到相同的错误。我使用 Django 1.11 和 Python 2.7
我重置了几次数据库,但没有用,数据库是 Postgres/sqlite。
unique_together
约束导致错误。去掉一样就好了