I have a Django application where users have additional data. That data is collected in a Profile model with a OneToOneField pointing to User.
This is fine and works perfectly for most purposes, but I have trouble customizing the admin for User. In particular:
I would like to be able to show a Profile field inside list_display. I don't know how to do this without writing an additional method on User itself.
I would like to be able to show some information about related models (e.g. some resources owned by the user) inside the User detail page. Again, I do not know how to do this without writing a custom User method.
Do you know any solution to the above?
You only have to edit the admin classes in admin.py. You can use admin.inline* class to help you. Example from Django website that will add Book to the Author's admin page:
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
admin.site.register(Author, AuthorAdmin)
Read more here.
EDIT: You should be able to add methods on UserAdmin model and refer to them when setting the list_display fields:
list_display = (..., 'your_method')
Turns out, one can put the methods in the UserAdmin itself instead than in the User model. This way I can access all the information I need about the user.
Related
I need to create an authentication method for use is_authenticated() and other methods from my templates. The problem is that I didnĀ“t understand the django documentation.
I have my User model and I wish to use it like the User model (like django uses its own model).
What should I do? Who can guide me? Thanks!
You must add a foreign key fields which refers to User class of Django.
Example :
from django.contrib.auth.models import User
class yourUserModel(models.Model):
user = models.ForeignKey(User, on_delete=True)
def your_authenticated_method():
self.user.is_authenticated()
I hope that I answered well at your question
When in a User object in the Django Admin, there is a really nice way to add permissions to the user.
For a completely different model, I would like to use the same system. This model has a manytomany field to another model. Is there a way to copy the layout of the user admin, to this model's admin?
Add your field to the ModelAdmin's filter_horizontal:
#admin.register(MyModel)
class MyModelAdmin(ModelAdmin):
# ...
filter_horizontal = ['my_many_to_many_field']
I'm new to Django and I want to create an app where artistes can post their songs and albums. Now I want artistes to have a different sign-up page from the normal users. I want artistes to be able to add their portraits, genres, and all that. Is there a way to add these fields to the User model? I've seen some questions on this but I don't think I really understood the answers.
There are basicly two ways to achive this:
1. Create a new model Artist with a OneToOneField to the django user model. This is most likely what you want. E.g. like this:
from django.contrib.auth.models import User
class Artist(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
genres = models.ManyToManyField('myapp.Genre', related_name='artists')
class Portrait(models.Model):
artist = models.ForeignKey('myapp.Artist', related_name='portraits')
class Genre(models.Model):
name = models.CharField(max_length=30)
2. Specify a custom User model that inherits from AbstractBaseUser. This is only reccomended if you want to store additional information related to authentication itself.
I suggest that you read the documentation on this carefully:
https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model
To create a custom sign-up page you will need to create your own FormView with a custom template e.g. using the django built in UserCreationForm and/or ModelForm. You could extend it with whichever fields you need. There are several ways to achive this depending on your needs.
I am really stuck in my project right now. I am trying to implement Oauth2 for my app. I found out about django-oauth2-provider a lot and tried it. The only problem is, it uses the User model at django.contrib.auth. The main users of our site are saved in a custom model called User which does not inherit from or extend the model at django.contrib.auth.
Is there any way to use my custom User model for creating clients and token?
If django-oauth2-provider can't be used for this purpose, can anyone recommend me some oauth2 library with the option to implement oauth2 with my own model.
Sincerely,
Sushant Karki
As the previous answer suggested, you should extend AbstractUser from django.contrib.auth.models.
The problem with the access token that the OP referring to, occur when changing the setting AUTH_USER_MODEL AFTER django-oauth2-provider was migrated.
When django-oauth2-provider is migrated, it creates a key constrain between the User model and django-oauth2-provider.
The solution is very easy:
Create your new User model and change the AUTH_USER_MODEL setting.
Go to the django_migration table in your database.
Delete all rows of django-oauth2-provider.
run python manage.py makemigrations
run python manage.py migrate
Now, the django-oauth2-provider tables are connected to the RIGHT User model.
django-oauth2-provider fetches the user model using settings.AUTH_USER_MODEL, with a fallback to auth.User. If you extend AbstractUser your User model will include all the fields of auth.User plus any additional fields you specify.
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
some_additional_field = models.BooleanField(default=False)
Specify the user model to be used like this in settings.py:
AUTH_USER_MODEL = 'user_api.User'
If you don't want to base your user on AbstractUser you'll also need to write your own user manager, e.g. by extending the BaseUserManager
You can read more about ways to customize django's user model here.
I have a form that is based on the model User from django.contrib.auth.models
I have created another model called UserProfile which contains more information about the user.
In my forms.py I have a form that is based on:
class Meta:
model = User
How can I show in my html the fields that is owned by UserProfile class?
PS.: In UserProfile class I have already created a field user = models.OneToOneField(User)
Thanks in advance!
Create two forms and display them both in the same <form> tag. Then manually check whether the forms are valid and call form.save() on both of them. It's a bit more work but perhaps cleaner than merging them forcefully into one form.
(credit)
Essentially what you need to look at is in-line forms. They cover it well for admin customization but the same principle applies when using ModelForms. Usually people just sub-class User?