Opened 14 years ago

Closed 14 years ago

#15833 closed Bug (needsinfo)

Exclude not working with ForeignKey in admin Inlnes

Reported by: claytongulick Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Normal Keywords:
Cc: mathieu.agopian@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a simple model:

class ImageFile(models.Model):
    name = models.CharField(max_length=50, help_text="The textual name for the image, maximum of 50 chars")
    gallery = models.ForeignKey(Gallery,help_text="Select the gallery that this image belongs to",blank=True,null=True)
    image_file = models.ImageField(upload_to="site/%s/images/"%(settings.SITE,),help_text="Select an image file to upload")
    display_order = models.IntegerField(blank=True,null=True,help_text="An optional field used for ordering the rows visually")
    content_type = models.ForeignKey(ContentType) #reference to the magic contentTypes table
    object_id = models.PositiveIntegerField() #the generic ID of the table we're relating to
    content_object = generic.GenericForeignKey('content_type','object_id') #a generic reference is a relation that defines a) the id of the table we refer to, and b) the id of the row we're rel

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['display_order']

Using that model as an inline, I want to exclude the gallery field, because it is optional, like this:

class ImageFileInline(generic.GenericTabularInline):
    model=ImageFile
    exclude=('gallery',)

That results in this error:

ImproperlyConfigured at /admin/consumer/product/add/
ImageFileInline cannot exclude the field 'gallery' - this is the foreign key to the parent model Gallery.

However, if I use this instead, it works fine:

class ImageFileInline(generic.GenericTabularInline):
    model=ImageFile
    fields=('name','image_file','display_order',)

If I can remove the field by not including it in the list of fields, I should also be able to exclude the field.

Thanks!

Change History (5)

comment:1 by anonymous, 14 years ago

Easy pickings: unset

Hello claytongulick,

I'm not 100% sure, but i believe you have an inlines attribute listing the ImageFile model in your GalleryAdmin (or whatever the name of your ModelAdmin for the Gallery Model is).

I tried reproducing this bug on trunk, and indeed, if i make ImageFile an inline on Gallery, and i exclude "gallery" from the ImageFileInline, i do get exactly the same error (and not if i use "fields" to take it out of the list). However, at least to me, this isn't a bug : you don't need to exclude the "parent" model from the inline, as it won't be displayed as a field to fill in.

When i made the ImageFile be an inline on some other model (making it a GenericTabularInline, i believe you want it to be an inline on the generic fk), i couldn't reproduce the problem anymore... until i snooped a inlines = [ImageFileInline] on the GalleryAdmin again, to try reproducing the bug.

To sum up

Could you please make sure you haven't got a misplaced ImageFileInline in your GalleryAdmin.inlines attribute?
If you definitely don't (make sure you're seeing the up to date code, by reloading/restarting your web server if needed), could you please provide some more content and code, to help reproducing the issue in the exact way you have it?

Thanks in advance

Mathieu

P.S: for reference, the error message comes from django.contrib.admin.validation.py, line 189 in validate_inline, and is supposed to check if there's an exclude only in the parent model of the inline

P.P.S: i'm not changing this ticket to "works for me" or "invalid" as

  • this is my very first ticket review
  • i'm not 100% sure i got (and understood) all the needed information to be certain that this isn't indeed a bug, as i had to infer and guess some of the context

comment:2 by Mathieu Agopian <mathieu.agopian@…>, 14 years ago

this is just there because i didn't fill in my name/email for the previous post :(

comment:3 by FunkyBob, 14 years ago

I can only assume you're trying to use this inline with the Gallery admin class [your ticket is far from detailed enough.]

Firstly, there's no point excluding the gallery field, as it will be omitted -- it's going to be set to the Gallery you're editing at the time.

However, I can see a case for suppressing the error, since you're asking it to exclude a field it would exclude anyway. That said, it's fair to warn you about pointless exclusion.

comment:4 by Mathieu Agopian, 14 years ago

Cc: mathieu.agopian@… added

comment:5 by Mathieu Agopian, 14 years ago

Resolution: needsinfo
Status: newclosed

I'm closing this ticket as "needsinfo", so please, if this is definitely a bug, feel free to reopen it with some additional information on how to reproduce this issue !

Thanks

Note: See TracTickets for help on using tickets.
Back to Top