edit model properties in the admin

534 views
Skip to first unread message

Andy

unread,
Feb 14, 2018, 6:05:38 AM2/14/18
to Django users
Im trying to have a custom admin that is also able to edit some model properties, but putting those properties into my fieldsets its complaining

Unknown field(s) (foo, bar) specified for Profile. Check fields/fieldsets/exclude attributes of class ProfileAdmin.

The properties work in list_display, but im missing something for the edit page.
Can i actually do this? So far i didnt find any hint on editing properties with the admin.

Gonzalo Delgado

unread,
Feb 14, 2018, 7:54:30 AM2/14/18
to django...@googlegroups.com
On 14/2/18 08:05, Andy wrote:
> Im trying to have a custom admin that is also able to edit some model
> properties, but putting those properties into my fieldsets its complaining

What would mean to edit a property (assuming this means an attribute
that isn't a model field) in your app? Where would the data go?

> Unknown field(s) (foo, bar) specified for Profile. Check fields/fieldsets/exclude attributes of class ProfileAdmin.
> The properties work in list_display, but im missing something for the edit page.


The Django admin will look only for Django fields in the edit forms,
that's why you're getting the errors.
Since list_display is only for showing data, it will display any
attribute in your model.

> Can i actually do this? So far i didnt find any hint on editing properties with the admin.

What you can do is use a custom model form and add fields to it for the
attributes you want to be able to "edit", but you'll also need to define
what happens with the data either in the custom form class itself, or in
the ModelAdmin class by extending the change_view method.

See the documentation for details:
https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form

--
Gonzalo Delgado

Andy

unread,
Feb 14, 2018, 7:59:44 AM2/14/18
to Django users
As an example i wanted to have the username editable inside the profile. Since the Django default user doesnt have a foreign key to profile i cant just use an inline admin for the user model, so my next idea was to solve it with properties just getting from and saving to the related user model.

Adler Neves

unread,
Feb 14, 2018, 8:45:49 AM2/14/18
to Django users
You can have an OneToOneField in the profile referencing Django's User model with `related_name='profile'`, so you can access information from there as `user.profile.birthday`. An useful link: https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

About the admin interface changing the model, I guess you're somehow editing `models.py` files. Are you making the migrations, applying them to the database and reloading the server? If you're not persisting changes that way, how do you patch models in memory every startup and ensure database schema is compatible with your models?

Andy

unread,
Feb 14, 2018, 9:01:15 AM2/14/18
to Django users


Am Mittwoch, 14. Februar 2018 14:45:49 UTC+1 schrieb Adler Neves:
You can have an OneToOneField in the profile referencing Django's User model with `related_name='profile'`,

Well, i know this and the related entry is accessible even without explicitly defining the related_name. You can change defaults defining the related_name, tho. 
But still trying to add a UserInline to the ProfileAdmin wont work due to the missing FK on the user model.

 

About the admin interface changing the model, I guess you're somehow editing `models.py` files. Are you making the migrations, applying them to the database and reloading the server? If you're not persisting changes that way, how do you patch models in memory every startup and ensure database schema is compatible with your models?


Just for clarification, i dont do any mysterious magic: the property works like this:

@property
def username(self):
    return self.user.username
username.fget.short_description = "Username"

@username.setter
def username(self, username):
    self.user.username = username
    self.user.save()


So with everything else failing .. although i only want to be able to edit this one field of the actual user, it looks like i have to show a useradmin with a profile inline admin?
AFAIK this can only be done with a separate admin site, because there can only be one Admin for any model thats why i wanted to have some other solution. 

Maybe someone knows a better solution?
 

 

Melvyn Sopacua

unread,
Feb 14, 2018, 11:29:13 AM2/14/18
to django...@googlegroups.com
On woensdag 14 februari 2018 15:01:15 CET Andy wrote:

> Well, i know this and the related entry is accessible even without
> explicitly defining the related_name. You can change defaults defining the
> related_name, tho.
> But still trying to add a UserInline to the ProfileAdmin wont work due to
> the missing FK on the user model.

Your reasoning is flawed. The model that is inlined never has a foreign key to
the model that it is inlined with. It is the reverse of the foreign key.
Foreign keys are not inlined, they are rendered via select widgets.

Something else is preventing this from working and without code and
corresponding backtrace we cannot diagnose what.

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages