Re: Admin list object, display fields from a fk object
I've updated a little models ( OneToOne field is better for your case) Take a look at
https://docs.djangoproject.com/en/dev/ref/models/fields/
result model is : https://docs.djangoproject.com/en/dev/ref/models/fields/
class B(models.Model):
""" Model B"""
keyfield = models.OneToOneField('mymodel.A')
name_b = models.CharField(max_length=20)
def __unicode__(self):
return "%s, %s" % (self.name_b, A.objects.get(id=self.id).name_a)
serg@debian:~/project/linkmodels/modeltest$ python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
serg@debian:~/project/linkmodels/modeltest$ python manage.py shell
Python 2.7.3 (default, Mar 5 2013, 01:19:40)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from mymodel.models import A, B
In [2]: A.objects.all()
Out[2]: [<A: object_A>]
In [3]: B.objects.all()
Out[3]: [<B: object_B, object_A>]
Many thanks,
Serge
+380 636150445
skype: skhohlov
Serge
+380 636150445
skype: skhohlov
On Tue, May 7, 2013 at 6:56 PM, alexandrerua@gmail.com <alexandrerua@gmail.com> wrote:
Thank you for your answer, that i managed to achive,what I want is to display on the admin object list of object B, display name_B and name_AAnywayThanksAlex
Terça-feira, 7 de Maio de 2013 13:43:59 UTC+1, Sergiy Khohlov escreveu:this not hard :
from django.db import models
# Create your models here.
class A(models.Model):
""" model A """
name_a = models.CharField(max_length=20)
def __unicode__(self):
return self.name_a
class B(models.Model):
""" Model B"""
keyfield = models.ForeignKey('mymodel.A')
name_b = models.CharField(max_length=20)
def __unicode__(self):
return self.name_b
serg@debian:~/project/linkmodels/modeltest$ python manage.py shell
Python 2.7.3 (default, Mar 5 2013, 01:19:40)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from mymodel.models import A, B
In [2]: A.objects.all()
Out[2]: []
In [3]: object_A = A('object_A')
In [4]: A.objects.all()
Out[4]: []
In [5]: object_A.save()
In [6]: object_B = B(name_b = 'object_B')
In [7]: object_B.keyfield = object_A
In [8]: object_B.save()
In [9]: B.objects.all()
Out[9]: [<B: object_B>]
In [10]: B.objects.filter(id=1)
Out[10]: [<B: object_B>]
In [11]: B.objects.filter(id=1)[0]
Out[11]: <B: object_B>
In [12]: B.objects.filter(id=1)[0].name_b
Out[12]: u'object_B'
In [13]: B.objects.filter(id=1)[0].keyfield
Out[13]: <A: object_A>
In [14]: B.objects.filter(id=1)[0].keyfield.name_a
Out[14]: u'object_A'Hiclass A(models.Model):f1=...f2=...f3=...class B(models.Model)f4=Foreignkey(A)f5=...f6=...class BAdmin(admin.ModelAdmin)list_display= (f4,f4__f2,f4__f3) # this does not workHow do I in the list display ob objects B, diaplay some fields from A linked by the foreign key.this syntax works for filter in class BAdmin!RegardsAlexandre Rua--To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home