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'



Many thanks,

Serge


+380 636150445
skype: skhohlov


On Tue, May 7, 2013 at 11:43 AM, alexandre...@gmail.com <
alexandre...@gmail.com> wrote:

> Hi
>
> class 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 work
>
> How 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!
>
> Regards
> Alexandre Rua
>
> --
> 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+unsubscr...@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+unsubscr...@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.


Reply via email to