Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » M2M with Intermediary - Django Admin Support - Doesn't appear? [RSS Feed]

#1 May 14, 2010 07:09:33

Victor H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

M2M with Intermediary - Django Admin Support - Doesn't appear?


heya,

I have a Django app that keeps track of newspapers articles, and the
companies mentioned in them (this is slightly simplified, but it'

Each article has a m2m relationship with firm. Originally, this was
just a direct m2m, however, I found I needed to add a "rating" to each
firm that's mentioned in an article, hence I created an intermediary
model (FirmRating), which then links Article to Firm.

The relevant models:

class Article(models.Model):
title = models.CharField(max_length=100)
publication_date = models.DateField()
abstract = models.TextField() # Can we restrict this to 450
characters?
category = models.ForeignKey(Category)
subject = models.ForeignKey(Subject)
source_publication = models.ForeignKey(Publication)
page_number = models.CharField(max_length=30)
article_softcopy = models.FileField(upload_to='article_scans',
null=True, blank=True, help_text='Optionally upload a soft-copy (scan)
of the article.')
url = models.URLField(null=True, blank=True, help_text =
'Enter a URL for the article. Include the protocl (e.g. http)')
firm = models.ManyToManyField(Firm, null=True, blank=True,
through='FirmRating')
#firm = models.ForeignKey(Firm)
spokesperson = models.ManyToManyField(Spokeperson, null=True,
blank=True)
#spokesperson = models.ForeignKey(Spokeperson)

def __unicode__(self):
return self.title

class FirmRating(models.Model):
firm = models.ForeignKey(Firm)
article = models.ForeignKey(Article)
rating = models.IntegerField()

class Firm(models.Model):
name = models.CharField(max_length=50, unique=True)
homepage = models.URLField(verify_exists=False,
help_text='Enter the homepage of the firm. Include the protocl (e.g.
http)')

def __unicode__(self):
return self.name

class Meta:
ordering =

And my admin.py:

class FirmRatingInline(admin.TabularInline):
model = FirmRating
extra = 1

class FirmAdmin(admin.ModelAdmin):
list_display = ('name', 'homepage')
list_editable = ('homepage',)
search_fields = ('name', 'homepage')
inlines = (FirmRatingInline,)

class ArticleAdmin(admin.ModelAdmin):
#inlines =
date_hierarchy = 'publication_date'
filter_horizontal = ('firm', 'spokesperson')
list_display = ('title', 'publication_date', 'category',
'subject', 'source_publication', 'page_number')
list_editable = ('publication_date', 'category', 'subject',
'source_publication', 'page_number')
list_filter = ('publication_date', 'category', 'subject',
'source_publication')
search_fields = ('title', 'publication_date', 'abstract',
'category__name', 'subject__name', 'source_publication__name',
'page_number', 'url')
inlines = (FirmRatingInline,)

I also read:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-modelswhich provided some info on getting Django admin to play nicely with
intermediary models.

However, I can't seem to get this working on here, I suspect there's
something wrong in the above code

Before, when I was editing articles in the admin (with just a straight
m2m), it offered adding firms to each article in a nice
filter_horizontal widget. However, now, with the intermediary model,
it's like Firm doesn't even exist to Articles anymore, there's no
mention of it on the Article editing page.

There's nothing relevant in the Apache error logs either. I'm a bit
stumped. Any ideas?

Thanks,
Victor

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 May 14, 2010 17:29:26

Nuno M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

M2M with Intermediary - Django Admin Support - Doesn't appear?


Hmmm ... you should have an inline formset at the bottom to add firms
to your Article, with an associated input box forthe rating.

If I understand correctly, each relation Article-Firm now needs a
rating (mandatory), sou you can't just select Firms from a
filter_horizontal widget - you need a way to input the rating as well.

Nuno

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 24, 2010 09:05:10

Victor H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

M2M with Intermediary - Django Admin Support - Doesn't appear?


heya,

Nuno: Thanks for the advice.

I know how to add normal inlines to the home page for a model, via the
"inlines" list in admin.py. However, how exactly do I add an inline
formset to the Add Article page?

I can override the template for the Add Article page, however, I don't
think that's what needs to be done here.

Is there a clean way of using the existing "Add Article" page, but
just swapping in an inline for the intermediary? Also, I'd like to
leverage off the exisiting "filter_horizontal" widget, if possible.
Basically, I just want it exactly as it was, without the intermediary,
with a filter_horizontal for settings the m2m relationship, but then
with just an additional widget for setting the intermediary "rating"
field.

(I found this write-uphttp://www.fictitiousnonsense.com/archives/22however, it seems geared towards replacing a single field on a single
model, not sure how it'd handle the intermediary model here. Also is
this method still valid/best practice?).

Cheers,
Victor

On May 15, 2:29 am, Nuno Maltez <nuno.li...@gmail.com> wrote:
> Hmmm ... you should have an inline formset at the bottom to add firms
> to your Article, with an associated input box forthe rating.
>
> If I understand correctly, each relation Article-Firm now needs a
> rating (mandatory), sou you can't just select Firms from a
> filter_horizontal widget - you need a way to input the rating as well.
>
> Nuno
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group
> athttp://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » M2M with Intermediary - Django Admin Support - Doesn't appear? [RSS Feed]

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support