#33907: Uniqueconstraints voided in when using GenericTabularInline
-------------------------------------+-------------------------------------
     Reporter:  Mohamed Alnahhas     |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:                       |                  Version:  4.0
  contrib.contenttypes               |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Mohamed Alnahhas:

Old description:

> Using Usecontraints, I get an error that the object already exist when I
> create a new object in the Skill_level admin page. However creating the
> same object through GenericTabularInline gives no errors and the creation
> is successful when it should not be. So basically Useconstraints is not
> working at all with GenericTabularInline
>
> Generic field model:
> {{{
> class Skill_level(models.Model):
>     level = models.IntegerField(default=1)
>     user = models.ForeignKey(User,
> related_name="skill_level",on_delete=models.CASCADE)
>     content_type = models.ForeignKey(ContentType,
> on_delete=models.CASCADE)
>     object_id = models.PositiveBigIntegerField()
>     content_object = GenericForeignKey("content_type", "object_id")
>

>     class Meta:
>         indexes = [
>             models.Index(fields=["content_type", "object_id"]),
>         ]
>         constraints = [
>             UniqueConstraint(fields=['user','content_type', 'object_id'],
> name='unique_object')
>         ]
> }}}
>

> Generic relation model:
>
> {{{
> class Subject(models.Model):
>     title = models.CharField(max_length=100)
>     skill_level = GenericRelation(Skill_level,
> related_query_name="subject")
>
>     def __str__(self):
>         return f"{self.title}"
> }}}
>
> Admin.py:
>
> {{{
> class Skill_levelInline(GenericTabularInline):
>     model = Skill_level
>     extra = 0
>
> class Skill_levelAdmin(admin.ModelAdmin):
>     model = Skill_level
>     fields = ['user', 'level', 'contnet_type', 'object_id',
> 'content_object']
>     readonly_fields = ["content_object"]
>
> class SubjectAdmin(admin.ModelAdmin):
>     inlines = [Skill_levelInline]
>     extra = 0
>
> admin.site.register(Skill_level)
> admin.site.register(Subject, SubjectAdmin)
> }}}

New description:

 Using Uniquecontraints, I get an error that the object already exist when
 I create a new object in the Skill_level admin page. However creating the
 same object through GenericTabularInline gives no errors and the creation
 is successful when it should not be. So basically Useconstraints is not
 working at all with GenericTabularInline

 Generic field model:
 {{{
 class Skill_level(models.Model):
     level = models.IntegerField(default=1)
     user = models.ForeignKey(User,
 related_name="skill_level",on_delete=models.CASCADE)
     content_type = models.ForeignKey(ContentType,
 on_delete=models.CASCADE)
     object_id = models.PositiveBigIntegerField()
     content_object = GenericForeignKey("content_type", "object_id")


     class Meta:
         indexes = [
             models.Index(fields=["content_type", "object_id"]),
         ]
         constraints = [
             UniqueConstraint(fields=['user','content_type', 'object_id'],
 name='unique_object')
         ]
 }}}


 Generic relation model:

 {{{
 class Subject(models.Model):
     title = models.CharField(max_length=100)
     skill_level = GenericRelation(Skill_level,
 related_query_name="subject")

     def __str__(self):
         return f"{self.title}"
 }}}

 Admin.py:

 {{{
 class Skill_levelInline(GenericTabularInline):
     model = Skill_level
     extra = 0

 class Skill_levelAdmin(admin.ModelAdmin):
     model = Skill_level
     fields = ['user', 'level', 'contnet_type', 'object_id',
 'content_object']
     readonly_fields = ["content_object"]

 class SubjectAdmin(admin.ModelAdmin):
     inlines = [Skill_levelInline]
     extra = 0

 admin.site.register(Skill_level)
 admin.site.register(Subject, SubjectAdmin)
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33907#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701827db930e5-9c560289-2100-427e-b10a-d635b598eaf0-000000%40eu-central-1.amazonses.com.

Reply via email to