首页 > 解决方案 > 如果我们从 html 输入新的输入,UUID 会重复吗?

问题描述

出现错误 - 我将一个表单从 html 保存到我的数据库中,而当我尝试保存下一个表单时,它给了我这个错误 - 客户处的 IntegrityError (1062, "Duplicate entry 'ad138e46-edc0-11va-b065-a41g7252ecb4'' for key 'customer .PRIMARY'") 请在 models.py 中解释我的 uuid 是 -

class customer(models.Model):
    customerid = models.CharField(default=str(uuid.uuid4()), max_length=500, primary_key=True)
    customername=models.CharField(max_length=100)

好心的帮助

更新

表格.py

class createcustomerform(ModelForm):
    class Meta:
        model=customer
        fields=[
'customername']

更新了 Models.py

import uuid
from uuid import UUID
from django.contrib.auth.models import User
from django.dispatch.dispatcher import receiver
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import pre_save
class customer(UUIDMixin,models.Model):
    customername=models.CharField(max_length=100)
    def __str__(self):
        return self.customername
class UUIDMixin(models.Model):
       uuid = models.UUIDField(blank=True,db_index=True,default=None,help_text=_('Unique identifier'),max_length=255,null=True,unique=True,verbose_name=_('UUID'))
       class Meta:
            abstract = True
@classmethod
def check_uuid_exists(cls, _uuid):
        #Determine whether UUID exists """
          manager = getattr(cls, '_default_manager')
          return manager.filter(uuid=_uuid).exists()
          
@classmethod
def get_available_uuid(cls):
        #Return an Available UUID """
          row_uuid = uuid.uuid4()
          while cls.check_uuid_exists(uuid=row_uuid):
            row_uuid = uuid.uuid4()
            return row_uuid       
@receiver(pre_save)
def uuid_mixin_pre_save(sender, instance, **kwargs):
    if issubclass(sender, UUIDMixin):
        if not instance.uuid:
            manager = getattr(instance.__class__, '_default_manager')
            use_uuid = uuid.uuid4()
        while manager.filter(uuid=use_uuid):
            use_uuid = uuid.uuid4()
            instance.uuid = use_uuid
            #Automatically populate the uuid field of UUIDMixin models if not already populated.

标签: djangodjango-modelsdjango-formsuuid

解决方案


您应该使用适当的UUIDField并避免设置默认值。

确保在创建对象时设置该值,并确保该值是唯一的 - 显然 UUID 中重复的机会非常小,这就是重点。

您可以自己创建一个模型 mixin,它将 a 添加uuid到您的模型中,并确保在保存对象时设置该值;



class UUIDMixin(models.Model):
    """
    Mixin for models contain a unique UUID, gets auto-populated on save
    """
    uuid = models.UUIDField(
        blank=True,
        db_index=True,
        # default=uuid.uuid4,
        # NB: default is set to None in migration to avoid duplications
        default=None,
        help_text=_('Unique identifier'),
        max_length=255,
        null=True,
        unique=True,
        verbose_name=_('UUID'),
    )

    class Meta:
        """Metadata for the UUIDMixin class"""
        abstract = True

    @classmethod
    def check_uuid_exists(cls, _uuid):
        """ Determine whether UUID exists """
        manager = getattr(cls, '_default_manager')
        return manager.filter(uuid=_uuid).exists()

    @classmethod
    def get_available_uuid(cls):
        """ Return an Available UUID """
        row_uuid = uuid.uuid4()
        while cls.check_uuid_exists(uuid=row_uuid):
            row_uuid = uuid.uuid4()
        return row_uuid


@receiver(pre_save)
def uuid_mixin_pre_save(sender, instance, **kwargs):
    """
    Automatically populate the uuid field of UUIDMixin models if not already
    populated.
    """
    if issubclass(sender, UUIDMixin):
        if not instance.uuid:
            manager = getattr(instance.__class__, '_default_manager')
            use_uuid = uuid.uuid4()
            while manager.filter(uuid=use_uuid):
                use_uuid = uuid.uuid4()
            instance.uuid = use_uuid

根据您的评论,让我解释更多。

上面是一个抽象模型,这意味着它本身不会创建表,它可以被其他(具体)模型使用,以便他们可以使用它定义的内容。

这是类和继承的好处。允许您不要在对许多模型有用的东西上重复代码。

不,在元数据中你会看到abstract = True。所以你定义你的模型,它会从这个抽象模型中MyModel(UUIDMixin, models.Model)获取一个字段以及你定义的任何内容。uuid

你会通过做这样的事情来使用它;

class Customer(UUIDMixin, models.Model):
    name = models.CharField(max_length=100)

如果你真的想使用 UUID 作为主键,可以设置两个 mixin, aPrimaryUUIDMixin和 the UUIDMixin,因为总的来说,主键上较小的值可能更有效。

你还提到Undefined variable: _

通常在 django 中,您会在文件顶部看到这样的导入;

from django.utils.translation import ugettext_lazy as _

然后将其用于包装字符串以便可以翻译它们,例如,_("Hello")

即使您不翻译您的项目,也通常只包含此内容。你可以在这里阅读;https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#standard-translation


推荐阅读