django模型具有两个字段的惟一组合

时间:2021-07-16 04:27:32

I have a django model as below

我有一个django模型,如下所示

class MySubject(models.Model):
    name=models.CharField(unique=True,max_length=50)
    description=models.TextField(blank=True)
    slug=models.SlugField(editable=False)

    class Meta:
        verbose_name_plural="MySubjects"

    def __unicode__(self):
        return self.name

    def save(self,*args,**kwargs):
        self.name=self.name.strip()
        self.slug=slugify(self.name)
        super(MySubject,self).save(*args,**kwargs)

    @models.permalink
    def get_absolute_url(self):
        return ('subject_detail',(),{'slug':self.slug})

I need to make the creator+ name unique ,sothat I can call

我需要使创建者+名称唯一,以便我可以调用

subject,status=MySubject.objects.get_or_create(name__iexact=name.strip(),creator= request.user,defaults={'name':name,'description':name,'creator':request.user})

Is the following ,the right way to do this?

下面的方法是正确的吗?

class MySubject(models.Model):
        name=models.CharField(max_length=50)
        creator = models.ForeignKey(User,null=True)
        description=models.TextField(blank=True)
        slug=models.SlugField(editable=False)

        class Meta: 
            verbose_name_plural="MySubjects"
            unique_together = ('name', 'creator',)
         ...

I guess I have to do a migration using south after making the change..Do I have to do schemamigration alone or do I have to do a datamigration?

我想在做了改变之后,我必须做一个移民。我需要单独做图式分解还是需要做数据分解?

1 个解决方案

#1


1  

Adding a unique constraint is a schema migration. However, if you have existing data that would cause an integrity error, you would need a data migration, as well.

添加唯一的约束是模式迁移。但是,如果您有可能导致完整性错误的现有数据,您还需要数据迁移。

If you really want case-insensitive unique constraint, it's a little more complicated:

如果你真的想要不区分大小写的唯一约束,那就有点复杂了:

Case insensitive unique model fields in Django?

Django中不区分大小写的惟一模型字段?

see also: https://code.djangoproject.com/ticket/14564

参见:https://code.djangoproject.com/ticket/14564

If you always use get_or_create with iexact, you may be ok. But, you should not manually create two with name as "foo" and "fOo", because, this would be allowed and then your call to get_or_create would cause a MultipleObjectsReturned .. if I'm thinking correctly.

如果您总是使用get_or_create与iexact,您可能不会有问题。但是,您不应该手工创建两个名为“foo”和“foo”的名称,因为这是允许的,然后您对get_or_create的调用将导致返回一个multipleobjectsreturn。如果我正确地思考。

#1


1  

Adding a unique constraint is a schema migration. However, if you have existing data that would cause an integrity error, you would need a data migration, as well.

添加唯一的约束是模式迁移。但是,如果您有可能导致完整性错误的现有数据,您还需要数据迁移。

If you really want case-insensitive unique constraint, it's a little more complicated:

如果你真的想要不区分大小写的唯一约束,那就有点复杂了:

Case insensitive unique model fields in Django?

Django中不区分大小写的惟一模型字段?

see also: https://code.djangoproject.com/ticket/14564

参见:https://code.djangoproject.com/ticket/14564

If you always use get_or_create with iexact, you may be ok. But, you should not manually create two with name as "foo" and "fOo", because, this would be allowed and then your call to get_or_create would cause a MultipleObjectsReturned .. if I'm thinking correctly.

如果您总是使用get_or_create与iexact,您可能不会有问题。但是,您不应该手工创建两个名为“foo”和“foo”的名称,因为这是允许的,然后您对get_or_create的调用将导致返回一个multipleobjectsreturn。如果我正确地思考。