python - IntegrityError with DateField - when creating objects with get_or_create -


i'm having issue django's get_or_create, when ever create same objects same dates integrity error pops up.

i have field in model follows.

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') 

error arises when try add same objects cart same from-date , until-date:

integrityerror @ /hoardings/hoardings-demo-2-5/add/ unique constraint failed: cart_cartline.cart_id, cart_cartline.date_from, cart_cartline.date_until 

get_or_create return integrityerror when creating objects same dates.i added unique features in datefield got same error. use django 1.11 , python 2.7

i reset database few times doesn`t help, db postgres/sqlite.

the unique_together constraint causing error. remove same , well.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -