django - add fields to model from another one related with OneToOneField or ForeignKey relation -


i new django , using 1.11 version
have several models related foreign keys , onetoone relation.
instance user , profile models, add in profile form fields user form
how?
onetoone, have admin model onetoone field related user model. not admin, have several types of user (admin, writer, commetator, ...) each in different model , when creating 1 type create related user, when access writer form in admin create admin want have user's model field create both writer's form
in adminadmin ie: admin model add user's fields in form showing in admin template

from django.contrib import admin  .models import user, admin   class userinline(admin.stackedinline):     model = user     fields = ['username', 'first_name', 'last_name']  class adminadmin(admin.modeladmin):     model = admin     list_display = ['getusername']     inlines = [userinline]      def getusername(self, obj):         return obj.user.username      getusername.short_description = "nom d'utilisateur"  admin.site.register(admin, adminadmin) 

this code generates error ": (admin.e202) 'common.user' has no foreignkey 'common.admin'."

with setup:

class a(models.model):     # ...     namefield = models.charfield(max_length=100, ...)     # ...     pass  class b(models.model)     # ...     fk = models.foreignkey(a)     # ...  class c(models.model):     # ...     oto = models.onetoonekeyfield(a) 

you cann access realted model's field foreignkey + __ + fieldname. e.g. can access model a's name field related models with:

b

'fk__name'

c

'oto__name'


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -