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 = ['name']
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 = [
#CategoryInline,
#]
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-models
which 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 at
http://groups.google.com/group/django-users?hl=en.