Django Admin modifying model inheritance - python

Hi All!
I have a model structured something like this
class BaseUser(models.Model):
user_data = models.ForeignKey(settings.AUTH_USER_MODEL) #External Auth User Model
class Teacher(BaseUser):
pass
class Student(BaseUser):
pass
And I am adding all the models to Django admin like so.
for model in get_models(get_app('MyApp')):
admin.site.register(model)
In the admin panel, I can create/view a list of BaseUser, Teacher, and Student. Where Teacher/Student are subsets of BaseUser.
The Question
When a new user is created, it is automatically a BaseUser.
Is there a way to change the class of an user from BaseUser to Teacher or Student in the admin panel?

No there's no builtin way to do it in admin. You either have to code it yourself or if you want to create Teacher do it from it's admin create view.
It's not only about python class of model but also database representation. For each model that use concrete inheritance, special table is created that holds additional fields for the model subclass and automatically created OneToOneField to parent. Details here.
Edit:
Try to specify parent link field which may be manageable from admin and so it'll allow you to create e.g. new Teacher linked with already existing BaseUser.
Edit:
Specifying parent link will not help as well because that field will not appear in admin.

Related

django how to load view for each user separatly? [duplicate]

Django - Models extension Vs User Profile
I want add some custom fields like following
1. ssn
2. is_manager
3. manager
I have 2 choices -
Extend AbstractBaseUser
OR
Create User profile based on signal and have OnetoOne field.
Which one is better, future proof, DB migration friendly and maintainable ?
The Django documentation answers this question in detail:
If you wish to store information related to User, you can use a OneToOneField to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.
In your case, the added fields do not seem to be authentication related, so your best bet is to use the user profile method. Substituting a custom user model is overkill for such purposes.

Is it possible to create new fields using django admin page interface?

Is it possible to create and delete new charfields or textareas through the Django admin page without harcoding them?
For example, I have a simple model, registered in Django admin page
class DocumentList(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
Obviously, it has only one charfield on admin page, something like:
DocumentList: [___________]
How can I add another one and delete her later if needed from Django admin page without actually hardcoding another charfield/textarea in models.py, to make it look like:
DocumentList: [___________]
*****************[___________]
Django models are not meant to be dynamically altered. You have to explicitly add the fields on your model, run migrations to have the fields created in your database backend, and reload your server process (./manage.py runserver does this automatically).
If you want to create a model that can hold an arbitrary amount of text strings instead of just one or a fixed amount, you need to use a many-to-many relation to another model.
You can use a custom form in the admin, either by using the form option of the get_form method. This is the documentation example for how you'd pass a custom form:
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm
You can add extra fields, as in any form.
I was wondering why you wanted this. Since you said in a comment it is to submit information to an API, you can also use an action, taking input from the user in an intermediate page.
EDIT: As became apparent in comments, the form needs to be dynamic for the user, and not when it is created. Therefore, the solution is using inlines, which once created and linked to the current model, allow the user to add any number of related forms to the current form.

Django custom user models in 1.8

I'd like to create a project for finding mentor.
In planning i thought, that it would be nice to separate two models on registration users: for students (those, who wants to find mentor) and mentors.
Built-in django user model isn't like that. I plan to add more fields, also several fields can be the same: in students and in mentors.
Can you give me live example of customing model? Would be nice, if you have smth in git or other code sharing.
Shoudl I inherit mentor model from students, because it can have same fields: email, name, surname, etc?
What additional code should i write for working custom model? I read docs and found unknown for me - managers. Should i also customize managers?
If i get success in custom model what problems can i meet in future for auth,registration, changing passwords for this custom model?
Creating 2 separate models is not recommended here. You will need to have separate login process and be careful to avoid problems with sharing pk between users in separate tables. Also I'm pretty sure that django won't allow that.
Better choice is to make 2 profile models, as described in Extending the existing User model. In one of profiles you will store specific data for student and in other specific data for mentors.
Your website has two intended users, so there is no problem with creating two user models. Just make sure to inherit them from user model
from django.contrib.auth.models import User
class Student(User):
...
class Mentor(User):
...
You shouldn't re-invent the wheel, except you really want to learn and practice core features of Django. Just add some add-on library like userena, which "supplies you with signup, signin, account editing, privacy settings and private messaging". In general userena gives an additional UserenaBaseProfile model which is connected to built-in User model. So you can just inherit this model for the Student and for the Mentor:
from django.contrib.auth.models import User
from userena.models import UserenaBaseProfile
class CustomProfile(UserenaBaseProfile):
user = models.OneToOneField(User, unique=True)
common_field_for_all_children = models.IntegerField()
class Meta:
abstract = True
class Student(CustomProfile):
something_student_related = models.IntegerField()
class Mentor(CustomProfile):
something_mentor_related = models.CharField(max_length=255)

django user and custom user class

In Django you have some naturally defined User class. My app also has a User class defined (they dont conflict, that's not the question)
My question is, since these two User classes conceptually represent the same thing (well, users) then it would be natural to integrate them. That is, have a single User class that contains all methods and variables of both classes.
What is the best way to achieve this?
There are (at least) two possibilities:
1) Use the 'custom user' functionality of Django (since Django 1.5), or
2) Use a OneToOneField to the django.contrib.auth User from your own user class.
The first allows you to customize more, but you might get some problems if you try to use third-party-apps that are either not ready for custom users or need specific properties of the stock User. For example, Django Guardian doesn't work if you remove the User-Group relationship.
The second is less intrusive, but doesn't allow you to customize the existing fields of User. Also, you need to manually create the instance of your own user class at registration time.
You should read the documentation about Extending the existing User model.
If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user. For example you might create an Employee (note: called MyUser below) model:
from django.contrib.auth.models import User
class MyUser(models.Model):
user = models.OneToOneField(User)
newfield1 = models.CharField(...)
AUTH_USER_MODEL = 'myapp.MyUser'

Adding a parent user in the user model in Django

I've read a few posts on inheritance of the User model for other models in Django, but I want a user to have a particular parent user, i.e. the User model inherits itself.
I'm not sure how to do this as I'd surely have to edit the django.auth.contrib model for User wouldn't I?
Thanks in advance for any tips.
Firstly, don't edit the source code in Django. That's a big mistake it'll make your code horrible to maintain - Whatever you're trying to do, you really don't want to change the Django source.
I'm not completely clear what you're asking, but it sound like you might need to create a new model that inherits from User and add a ManyToMany field to self on your model.
from django.contrib.auth.models import User
class ExtendedUser(User):
parent = models.ManyToManyField("self", symmetrical=False)
Your new model will now have all the fields and methods of Django's User class plus your extra parent field. The parent field on any particular ExtendedUser object will reference another ExtendedUser object.
As I say I'm taking a guess at what it is you're trying to do (If I've got it right, then you might find it helpful to do a bit of reading on model inheritance and class inheritance in general - maybe seek out a few tutorials on the subject too)

Categories

Resources