Tuesday, October 1, 2013

Re: In the Admin how do I display the target data instead of the m2m intermediary data


On Wed, Oct 2, 2013 at 6:19 AM, Mike Dewhirst <miked@dewhirst.com.au> wrote:
Consider a bunch of substances and a bunch of papers written by various researchers. Some of the papers apply to more than one substance and a substance can have multiple papers. Here are the models ...

class Paper_Substance(models.Model):
    paper = models.ForeignKey('Paper', null=True, blank=True)
    substance = models.ForeignKey('Substance', null=True, blank=True)
    reference = models.CharField(max_length=8, null=True, blank=True)
    ...

class Paper(models.Model):
    papers = models.ManyToManyField('Substance', through='Paper_Substance')
    ...
    title = models.CharField(max_length=200)
    url = models.URLField(null=True, blank=True)
    ...

In the Admin I want to add none, one or more Papers to any Substance. I can do this but I can only see the the content of Paper_Substance records instead of Paper records. In particular I want to display the URL of the paper. This is the admin snippet ...

class PapersInline(admin.StackedInline):
    model = Paper.papers.through
    fk_name = 'substance'

Is this possible?

Well… maybe.

The easy answer is no. You can't display an inline for the Paper model directly because an intermediate model is involved. Even though the 'extra' data is nullable, Django's admin forces the option to be displayed. This is a known limitation of the intermediate models in general; there are tickets floating around to address this limitation.

Depending on what is actually acceptable as a final solution, you *might* be able to work around the problem, though. If the only requirement is that you display the URL for the related Paper instance, you might be able to write a custom ForeignKey widget that the Inline will use to display the remote Paper instance, and modify the output of that widget to display whatever details about the Paper instance you want. It's not going to be simple, and it probably won't be pretty either, but it should be *possible*. 
 
Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJxq84_8U0iqUGB_SVKG9KkvQkEFKtPrKr_vfiq72x-wmTtn6g%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment