Sorry for the dumb question (and I feel that it is dumb). I only comprehend Django.

When I try to save the created promo , I get this error:

The database backend doesn’t accept 0 as a value for AutoField.

My model:

 # -*- 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 is the reason for this error?


 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 
  • one
    What is the database? What request leaves? Probably a problem in TreeOneToOneField. Look for whether this field in the database values ​​"0". - Chikiro pm
  • @Chikiro I don't have any entries in the Promo table yet. Maybe try to remove the default from there? All the same there is blank=true . - Viktor
  • show create table show, the error is connected with the date_end field? mysql? - strangeqargo

2 answers 2

Autofield is usually generated automatically. The reason for the error may be that you pass id = 0 somewhere when creating an instance of the model. There are no more suspicious places in the code you provided.

  • First, show the code that creates an instance of the model - views.py or from the console, if manually
  • create is better to render to your own Manager , and not to sculpt it as a classmethod

    Thanks @FeroxTL for the tip

    The problem was in this line:

     category = TreeOneToOneField(Category, verbose_name=_('category'), blank=True, default=0) 

    Here, when creating, category 0 transferred by default, which was not in my database. Simple removal of default=0 helped fix the problem.

    At the same time rewrote create . Here is what happened:

     class PromoManager(models.Manager): def create_promo_code(self, discount_money): promo = self.create(discount_money=discount_money) return promo class Promo(models.Model): promo = models.CharField(_('promo'), max_length=50, blank=False, default=id_generator, primary_key=True) 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) date_end = models.DateTimeField(_('Date end'), default=timezone.now) class Meta: verbose_name = _('promo') verbose_name_plural = _('promo') def __unicode__(self): return self.promo objects = PromoManager()