如果我们从 html 输入新的输入,UUID 是否重复?

Duplicate UUID if we enter a new input from html?

出现错误 - 我将 html 中的一份表格保存到我的数据库中 而当我尝试保存下一个时,它给了我这个错误- 客户的 IntegrityError (1062,“密钥 'customer.PRIMARY' 的重复条目 'ad138e46-edc0-11va-b065-a41g7252ecb4'”) 请解释我在 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)

请帮忙

已更新

Form.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.

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

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

您可以自己创建一个模型 mixin,它将 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

根据你的评论,我再解释一下。

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

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

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

您可以像这样使用它;

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

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

您还提到了Undefined variable: _

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

from django.utils.translation import ugettext_lazy as _

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

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