abraham
abraham

Reputation: 31

django ForeignKey model filter in admin-area?

Hi I need really very very simple example. First my models:

#This my student models
from django.db import models
SEX_CHOICES= (
    ('M', 'Male'),
    ('F', 'Female'),
)
class Students(models.Model):
    student_name = models.CharField(max_length=50)
    student_sex = models.CharField(max_length=8, choices=SEX_CHOICES)
    student_city = models.Charfield(max_length=50)
    student_bio = models.TextField()

    def __unicode__(self):
        return self.student_name

O.K. Let see my Classes Model.

#This my Classes models
from django.db import models
from myproject.students.models import *
class Classes(models.Model):
    class_number= models.CharField(max_length=50)
    class_student_cities = models.ForeignKey(Students)
    class_year = models.DateField()

    def __unicode__(self):
        return self.class_number

My classes/admin.py file looks like that:

from myproject.classes.models import *
from myproject.students.models import *
from django.contrib import admin

class ClassesChoiceField(Students):
    class_student_cities = Classes.objects.get(id=1).class_student_cities.student_city

admin.site.register(Classes)

I get this error:

DoesNotExist at /admin/classes/classes/add/
Classes matching query does not exist.

How can i show in the class_student_cities area the Students.student_city datas? I guess that about django-admin area. When i do it with ForeignKey(Students) i just see in that area the Students.student_name data :S. I'm really wondering how can I do it? Can you give me a little example? Many Thanks!

Upvotes: 0

Views: 400

Answers (3)

Sergei Sheludchenko
Sergei Sheludchenko

Reputation: 56

To get student_city from queryset, you can use:

Classes.objects.get(id=1).class_student_cities.student_city

And if you want to relate your foreignkey field not to primary key, you should use to_field argument

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.to_field

It will be like:

class_student_cities = models.ForeignKey(Students, to_field='student_city')

Upvotes: 1

Williams
Williams

Reputation: 4328

There are a few problems -- basically things are 'not quite right', which is why you keep being referred to the docs.

Here is an example of what an admin.py should look like:

from django.contrib import admin
from articles.models import Article

def show_articletype_thumbnail(self):
    return self.image.admin_thumbnail()
show_articletype_thumbnail.allow_tags=True
show_articletype_thumbnail.short_description = 'Image'


class ArticleAdmin(admin.ModelAdmin):
    save_on_top = True
    list_display = ['status', 'articletype', 'issue', 'penname', 'issue', show_articletype_thumbnail]
    list_display_links = ['articletype']
    list_filter = ['articletype', 'allow_comments', 'template', 'issue']

admin.site.register(Article, ArticleAdmin)

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599630

See the documentation.

Upvotes: 2

Related Questions