Generic many-to-many relationships in django admin

后端 未结 2 477
粉色の甜心
粉色の甜心 2021-02-10 12:18

I have few similar models in Django:

class Material(models.Model):
    title = models.CharField(max_length=255)
    class Meta:
        abstract = True

class Ne         


        
相关标签:
2条回答
  • 2021-02-10 12:33

    EDIT: Check this out http://charlesleifer.com/blog/connecting-anything-to-anything-with-django/

    GenericForeignKey's are unfortunately not as well supported as ForeignKey's. There's an open (and accepted) ticket with patch for providing a widget for them: http://code.djangoproject.com/ticket/9976

    What is provided out-of-the-box is managing objects with GenericForeignKey inline.

    Assuming your generic relationship is achieved by

    from django.contrib.contenttypes import generic
    from django.contrib.contenttypes.models import ContentType
    from django.db import models
    
    class News(models.Model):
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey('content_type', 'object_id')
        ...
    

    and

    class Topic(models.Model):
        ...
        news = generic.GenericRelation('News')   # if separate app: 'newsapp.News'
    

    If you want to edit the News of a Topic, you can define an inline admin for News:

    from django.contrib.contenttypes.generic import GenericTabularInline
    
    class NewsInline(GenericTabularInline):
        model = News
    

    and add it to the inlines of Topic admin:

    class TopicAdmin(models.ModelAdmin):
        inlines = (NewsInline, )
    

    That said, from the information given I can't see what's wrong with your ManyToMany relationship. It seems to express what you need.

    Maybe you're defining the ManyToMany field in Topic instead of in News and Article? Define them in News and Article.

    EDIT: Thanks for the clarification. Your model setup would be as per arie's post (i.e., the other way around) and you'd be editing inline. If you just want to select existing Topic's from inside a News/Article/etc. instance, I'm not aware of anything out-of-the-box for GenericRelation (which usually just serves as a reverse-lookup helper). You could

    a) Override the admin form and add a ModelMultipleChoiceField with the queryset as per the GenericRelation

    b) Override save() to adjust the relations.

    Quite a lot of work. I would personally stick with multiple m2m tables and not cram everything into one. If you are afraid of the database doing multiple lookups when you ask for all News and Articles and etc. of one or more Topic's, then be aware that a generic solution will always have a similar setup to the requirements GenericForeignKey has, i.e. additional columns for model and id. That could lead to a lot more queries (e.g. against content_type for each result).

    0 讨论(0)
  • 2021-02-10 12:41

    Shouldn't it work if you just turn Danny's example around and define the generic relation on the side of of the Topic-Model?

    See the example in django's docs: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline

    Adapted to this question:

    class Topic(models.Model):
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey("content_type", "object_id")
    

    It probably makes sense to additionally define the reverse relationsship on each related model.

    class News(models.Model):
        topics = generic.GenericRelation(Topic)
    

    And now you could create a TopicInline and attach Topics to news, articles, whatever ...

    class TopicInline(generic.GenericTabularInline):
        model = Topic
    
    class ArticleAdmin(admin.ModelAdmin):
        inlines = [
            TopicInline,
        ]
    
    class NewsAdmin(admin.ModelAdmin):
        inlines = [
            TopicInline,
        ]
    
    0 讨论(0)
提交回复
热议问题