views:

467

answers:

3

I have a manytomany relationship between publication and pathology. Each publication can have many pathologies. When a publication appears in the admin template, I need to be able to see the many pathologies associated with that publication. Here is the model statement:

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)
    def __unicode__(self):
        return self.pathology
    class Meta:
        ordering = ["pathology"]

class Publication(models.Model):
    pubtitle = models.TextField()
    pathology = models.ManyToManyField(Pathology)
    def __unicode__(self):
        return self.pubtitle
    class Meta:
        ordering = ["pubtitle"]

Here is the admin.py. I have tried variations of the following, but always get an error saying either publication or pathology doesn't have a foreign key associated.

from myprograms.cpssite.models import Pathology
class PathologyAdmin(admin.ModelAdmin):
    # ...
    list_display = ('pathology', 'id')

admin.site.register(Pathology, PathologyAdmin)

class PathologyInline(admin.TabularInline):
    #...
    model = Pathology
    extra = 3

class PublicationAdmin(admin.ModelAdmin):
    # ...
    ordering = ('pubtitle', 'year')
    inlines = [PathologyInline]
admin.site.register(Publication,PublicationAdmin)

Thanks for any help.

+1  A: 

Unless you are using a intermediate table as documented here http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models, I don't think you need to create an Inline class. Try removing the line includes=[PathologyInline] and see what happens.

Yes, I've tried removing the inlines=[PathologyInline] from PublicationAdmin and nothing appears. The intermed table just doesn't seem to get recognized in admin. Using from mysite.models import publication_pathology I get an error that says the name is not recognized.
What is the exact error you are getting? Sounds like you're having trouble importing modules in Python which has nothing to Django. The example you gave above doesn't make any sense because there is no module named 'publication_pathology'
The publication_pathology is the intermed table and I realize now it cannot be brought into the admin.py using the "import". The issue is that my data entry staff cannot select the dropdown pathology menu on the publication record. I see the entire unclickable pathology list, instead.
When I take control and create the intermed table then my admin.py works with inline, but then my html template doesn't drill down to publication through the intermed table.
When I let django create the intermed table then my html template works, but I can't get the admin.py to work. Urghh, it is really frustrating and I've spent days making and remaking tables.
I've also tried using the through command for manytomany, but it cannot be done in the order required. (Publication with MtoM to Pathology with a through to intermed table.). The intermed table with two foreign keys has to follow Publication.
Publication cannot see the intermed table it follows it.
If the problem is just the order of declerations, you can declare like this using the string of the Model name instead of a real reference models.ManyToManyField('Pathology')
Also, a ManyToMany field is not going to use a drop-down box, because more than one Model could be selected. I believe the default admin widget for a ManyToMany is a list-box that you can control-click on to select more than one Model.
Hello Gerdemb, Can you help me write this command:technology_id=request.POST['technology_id'] #retrieves users selectiont=get_object_or_404(Technology, pk=technology_id) # retrieves idProblem:Techpubcombo (intermed table) technology_id retrieves
the publication_id from this table to getid, title, et. How do I write the command. I triedpub=t.publication.techpubcombo_set.all Error " no attribute in Queryset"Thanks!
A: 

Hello,

I realize now that Django is great for the administration (data entry) of a website, simple searching and template inheritance, but Django and Python are not very good for complex web applications, where data is moved back and forth between a database and an html template. I have decided to combine Django and PHP, hopefully, applying the strengths of both. Thanks for you help!

A: 

That looks more like a one-to-many relationship to me, tho I'm somewhat unclear on what exactly Pathologies are. Also, so far as I understand, Inlines don't work on manytomany. That should work if you flip the order of the models, remove the manytomany and add a ForeignKey field to Publication in Pathology.

class Publication(models.Model):
    pubtitle = models.TextField()
    def __unicode__(self):
        return self.pubtitle
    class Meta:
        ordering = ["pubtitle"]

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)
    publication = models.ForeignKey(Publication)
    def __unicode__(self):
        return self.pathology
    class Meta:
        ordering = ["pathology"]
Adam Benzan