The database backend does not accept 0 as a value for Auto Field



  • I'm sorry about the stupid question. I'm just catching Django.

    When I'm trying to preserve the creature. promoThat's a mistake.

    The database backend does not accept 0 as a value for Auto Field.

    My model is:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    

    from django.db import models
    from django.utils import timezone
    from django.utils.translation import ugettext_lazy as _, pgettext_lazy
    from django.conf import settings
    from django.contrib.auth.signals import user_logged_in
    from django.contrib.sites.models import Site
    import string
    import random
    from catalog.models import Category
    from mptt.forms import TreeNodeChoiceField
    from mptt.models import TreeOneToOneField

    def id_generator(size=16, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

    class Promo(models.Model):
    promo = models.CharField(('promo'), max_length=50, blank=False, default=id_generator)
    discount = models.DecimalField(
    ('Discount %'), max_digits=14, decimal_places=2, default=0, blank=True)
    discount_money = models.DecimalField(('Discount (rub)'), max_digits=14, decimal_places=2, default=0, blank=True)
    order_id = models.CharField(
    ('order number'), max_length=50, blank=True, default=0)
    first_name = models.CharField(('first name'), max_length=30, blank=True)
    last_name = models.CharField(
    ('last name'), max_length=30, blank=True)
    part_number = models.CharField(('part number'), max_length=255, blank=True)
    category = TreeOneToOneField(Category, verbose_name=
    ('category'), blank=True, default=0)
    date_end = models.DateTimeField(_('Date end'), default=timezone.now)

    class Meta:
        verbose_name = _('promo')
        verbose_name_plural = _('promo')
    
    def __unicode__(self):
        return self.promo
    
    @classmethod
    def create(cls, discount_money):
        promo = cls(discount_money=discount_money)
        promo.save()
    
        return promo
    

    What's the reason for this mistake?


    def subscribe(self, email):
    self.model_class.objects.get_or_create(
    email=email,
    )
    self.create_promo()

    @staticmethod
    def create_promo():
    promo = Promo.create(config.SUBSCRIPTION_DISCOUNT)
    print promo


  • QA Engineer

    https://docs.djangoproject.com/en/dev/ref/models/fields/#autofield usually generated automatically. The reason for the error is that you're sending id=0 somewhere when you're creating a model copy. There's no longer any suspect spots in your code.

    • First, show the code that creates a copy of the model - views.py or consols, if manually
    • create better to face it. https://docs.djangoproject.com/en/1.9/topics/db/managers/#custom-managers , not blinding him as classmethod

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2