Code Monkey home page Code Monkey logo

Comments (4)

gasman avatar gasman commented on August 18, 2024

Is this feature really needed, or can the same thing be done with a ParentalKey on the through model (i.e. the way we 'faked' M2M relationships before ParentalManyToManyField was implemented)...?

from django-modelcluster.

m1kola avatar m1kola commented on August 18, 2024

This feature is really useful for projects with models that were implemented before modelcluster/Wagtail introduced M2M support. So models like

class BarToFoo(models.Model):
    foo = models.ForeignKey('appname.FooModel', blank=True, null=True)
    page = models.ParentalKey('BarPage', related_name='something')

can be modified to become:

class BarToFoo(models.Model):
    foo = models.ForeignKey('appname.FooModel', blank=True, null=True)
    page = models.ForeignKey('BarPage', related_name='+')

Then you just need to add something = ParentalManyToManyField(..., through='appname.BarToFoo') and replace InlinePanel in the "target" model with FieldPanel. No need to write data migrations.

It also allows developers to add additional data into through models.

from django-modelcluster.

mjpieters avatar mjpieters commented on August 18, 2024

A through model is a requirement when you want to record additional information about the relationship.

I need to record a rich text description per many-to-many relationship, the (source, target) tuples must be unique, and I need to put minimum and maximum limits on the relationship (each source needs to have at least 1, at most 8 such relationships recorded).

I guess I can achieve this with an inline panel and a full-blown model with a unique_together constraint, and some way to limit the available choices based on what's already present when editing? The latter would become rather hackish, I fear.

from django-modelcluster.

mihow avatar mihow commented on August 18, 2024

The syntax for ParentalManyToManyField(to=FooModel, through=BarToFoo) seems more Django-like rather than only defining the intermediary model and setting a ParentalKey as seen in the Wagtail examples.

Also it looks like Django just merged better support for updating m2m relationships that have an intermediary model as well: django/django#8981. So for the example in the ticket description you will be able to use bar_page.asset_programs.add(foo) even with a through model!

from django-modelcluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.