0

Below is a working code. I am creating models that can have multiple images, with using generic fields. And i am using inlines at the admin side. I need a way to get the model's (that can have multiple images ) class name within the inlineAdmin class in order to make this system more portable...

#model mediaalbums
class MediaAlbum(models.Model):
    content_type = models.ForeignKey(ContentType, null=True, blank=True)
    media_type = models.CharField(max_length=5, choices= MEDIA_TYPE_CHOICES ) 
    name = models.CharField(unique = True, max_length=50)

class ModelImage(Media): 
    album = models.ForeignKey(MediaAlbum, null=True, blank=True, limit_choices_to = {'media_type': 'image'})
    file = ImageField(upload_to=get_path) 
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id') 

#model sections
class Work(models.Model):
    images = generic.GenericRelation(ModelImage)

#admin
class ModelImageInline(generic.GenericStackedInline):
    model = ModelImage
    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        field = super(ModelImageInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
        if db_field.name == 'album':
                # for now i am getting the model's (that can have any number of images )           class name from the model's admin class' request object
                # but i do not want to get it from there
                className = request.className
                field.queryset = field.queryset.filter(content_type__name=className)    #building__exact = request._obj_
        return field


class WorkAdmin(TranslationAdmin):
    inlines = [ModelImageInline ]
    def get_form(self, request, obj=None, **kwargs):
        # pass the class name to inline class
        request.className = 'work'
        return super(WorkAdmin, self).get_form(request, obj, **kwargs)
4
  • I don't think the request has a parentClassName attribute. Commented Apr 16, 2013 at 14:03
  • I did'nt understand what are you really trying to do. Do you want the parent class of what? Commented Apr 16, 2013 at 14:04
  • Looks like album is a ForeignKey to an Album model which has a Generic key. However I don't understand, does the current code work ? Commented Apr 16, 2013 at 14:36
  • Code works fine... I have edited my post in order to be clearer. Commented Apr 16, 2013 at 15:16

2 Answers 2

4

ANSWER : use self.parent_model.__name__ within the inline class.

Sign up to request clarification or add additional context in comments.

Comments

1
parent_class = db_field.rel.to
parent_meta = parent_class._meta
app_label, object_name = parent_meta.app_label, parent_meta.object_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.