Many-to-many relationships in Django Admin - django

models:
class Detail(models.Model):
def __unicode__(self):
return self.title
title = models.CharField(max_length=32)
class Cars(models.Model):
def __unicode__(self):
return self.name
name = models.CharField(max_length=32, unique=True)
details = models.ManyToManyField(Detail)
So, every car has a many details - wheels, engine, etc. How to do this: in Django Admin situated Cars menu, in that menu we have a many lines of details (like in tutorial).
In admin I use:
class DetailInline(admin.TabularInline):
model = Detail
extra = 6
class CarsAdmin(admin.ModelAdmin):
inlines = [DetailInline]
But it has error: Detail has no ForeignKey to Cars. How to fix it?

Django does not natively let you add a reverse inline.
i.e. You can have the Detail page contain an inline admin of all the Cars that contain a ForeignKey to that particular Detail. However, the reverse is not natively possible.
There is a workaround though wherein you have to override the admin template a bit. There is a previous SO question about this here: Inline-like solution for Django Admin where Admin contains ForeignKey to other model

Related

How to show the correct name object in Django admin instead 'XXX object'

I've two apps in my Django Project and I created an ManytoManyField relationship between then. However, when I check it on my admin site, the references appears showing the model name plus object inside the Many to Many box.
I already tried write a str in my model as you can see below:
def __str__(self):
return str(self.course.name)
Below there is my models code.
from django.db import models
from cursos.models import Course
class Person(models.Model):
name = models.CharField(max_length=50)
course = models.ManyToManyField(Course, blank=True)
def __str__(self):
return str(self.course.name)
This part of the form deals with editing the ManyToManyField. It will use the __str__ of the model that is referenced.
In order to thus change the textual representation, you need to implement the __str__ of the Course model, like:
class Course(models.Model): # Course model, not Person
name = models.CharField(max_length=128)
def __str__(self):
return self.name
Setting the __str__ of a person to self.course.name does not seem correct, since you probably do not want to represent a Person by the name of its courses. Furthermore since this is a ManyToManyField, it will not work anyway.
Note: usually the name of ManyToManyFields is plural, so courses, instead of course, since it is basically a collection of Courses, not a single Course.

Django Admin Extended View

I have built out a Django voting application. The models are pretty simple.
I have a category , an entry , and participant model
class Category(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField()
class Participant(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
class Entry(models.Model):
votes = models.IntegerField()
category = models.ForeignKey(Category)
participant = models.ForeignKey(Participant)
def __unicode__(self):
output = 'Entry For {0} in Category {1}'.format(self.participant, self.category)
return output
Pretty straightforward. You can add a category an entry and a participant using the default django admin models. This works really well.
Now the question:
In the admin, I want user to click a button and is presented with the listing of all winners for all the categories in the db. I have an idea on how to implement this, where I basically want a user to submit a form in the admin interface. I know the admin interface is implemented via the way all djanog apps are MVC style. But I don't know where I can extend because the adminBaseModel / adminModel acts like a models and view controllers, and url-confs at the same time. It's seems difficult to rewire alot of the internets there.
Can someone point me in the right direction? Just want to simply implement my own view that merely extends the admin view with my own context and method calls.
I hope that was clear. Thanks for all your help guys.

Django Admin Select Objects based on ManyToMany Relationship

I'm working on building a django app that extends the Mezzanine project. Mezzanine has a Gallery app (photos). I'd like to create "portfolio" page that acts as a landing page which has a single image and link to each gallery page.
Each gallery (Gallery) can have multiple images (GalleryImage). I'd like to via the admin select a gallery, then select an image to be displayed. However, I can't seem to figure out what to do.
Here's my model:
class GalleriesThumb(models.Model):
relatedlandingpage = models.ForeignKey(LandingPage)
relatedgallery = models.ForeignKey(Galleries)
thumb = models.ManyToManyField(GalleryImage)
def __unicode__(self):
return self.name
class Galleries(models.Model):
landingpage = models.ForeignKey(LandingPage)
tagline = models.CharField(max_length=100)
galleries = models.ManyToManyField(Gallery)
def __unicode__(self):
return self.name
class LandingPage(models.Model):
gallerytitle = models.CharField(max_length=200)
def __unicode__(self):
return self.name
My admin is something like:
class GalleryInline(admin.InlineModelAdmin)
model = Galleries
model = GalleriesThumb
list_display = galleries
list_display = thumb
class LangingPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': ('gallerytitle')
})
inlines = [GalleryInline,]
I realized that this won't do what i want, but how do I get the list_display on the the images that are related to Galleries. I'm pretty sure it needs to be a method, or am I taking a completing wrong approach if the selections that are made will be defining the content on the page. (I realize that I'm also missing my fields to store the selection in.)
I'm sorry if this a dumb question, but this my first real world attempt an app.
I think this link will resolve your problem
Django 1.2.1 Inline Admin for Many To Many Fields
class GalleryInline(admin.InlineModelAdmin)
model = Galleries.galleries.through

django 1.2.x admin inline with custom relationship

I have 2 models:
models.py:
class Teacher(models.Model):
user = models.ForeignKey(User)
...
class Record(models.Model):
user = models.ForeignKey(User)
...
and admin.py
class RecordInline(admin.StackedInline):
model = Record
class TeacherAdmin(admin.ModelAdmin):
inlines = [RecordInline]
Basically what I want to do is to display records where users are same (teacher.user == record.user) inline in Teacher's detail in admin. The problem is that Teacher and Record do not have direct relationship, so I'm looking for a way to define this relationship.
How can I achieve this? I amd using Django 1.2.5 in my project
You could override one of the ModelAdmin's methods, eg get_inline_instances
class TeacherAdmin(admin.ModelAdmin):
inlines = [RecordInline]
def get_inline_instances(self, request):
# ...

Django ManyToManyField doesn't highlight selected items

I'm trying to add a ManyToManyField to my Django app and it almost works. My only problem is that when I've saved an object and view it again in the admin the ManyToMany-fields doesn't get selected.
I set blank=True since otherwise it wouldn't let me save without selecting at least one item and I want the many-to-many-fields to be optional.
The whole many-to-many admin field is greyed out, that might have something to do with it.
http://dl.dropbox.com/u/3184097/manytomany.png
Model:
class Disease(models.Model):
name = models.CharField(max_length=100)
text = models.CharField(max_length=2000)
vaccines = models.ManyToManyField(Vaccine, blank=True)
countries = models.ManyToManyField(Country, blank=True)
def __unicode__(self):
return self.name
Admin:
from dbaccess.models import *
from django.contrib import admin
admin.site.register(Vaccine)
admin.site.register(Disease)
admin.site.register(Country)
admin.site.register(Medicine)
EDIT:
I checked and the disease_vaccine and disease_countries does contain items, so they are saved just not shown when the Disease is opened again in the Admin.
Try doing:
class DiseaseAdmin(admin.ModelAdmin):
pass
admin.site.register(Disease, DiseaseAdmin)