0

I've created the following model:

school/models.py:

from django.contrib.auth.models import User
(...)
class Parent(User):
    contract = models.ForeignKey(Contract)
    user = models.OneToOneField(User, parent_link = True, related_name = 'school_parent')

Now I'm trying to "promote" a regular django user into a school parent:

>>> from django.contrib.auth.models import User
>>> from school.models import Parent, Contract
>>> u = User(username = 'myuser')
>>> u.save()
>>> User.objects.all()
[<User: myuser>]
>>> c = Contract.objects.get(pk = 1)
>>> p = Parent(user = u, contract = c)
>>> p.save()
>>> User.objects.all()
[<User: >]
>>> 

Apparently, in "Parent" creation, the user "myuser" is being destroyed. Django docs show[1] that you can "attach" one model to other via OneToOneField the way I'm doing. It also says that multi-table inheritance automatically creates a OneToOneField[2].

Is there a way to "promote" a User to Parent in this case? What about demotion?

Reference:

  1. https://docs.djangoproject.com/en/1.6/topics/db/examples/one_to_one/
  2. https://docs.djangoproject.com/en/1.6/topics/db/models/#multi-table-inheritance
2
  • Why did you chose to mix inheritance and OTO field? Is there a good reason to that? Just learned it was possible in Django, but can't see the point.. Commented Sep 9, 2014 at 15:04
  • Inheritance has an implicit OTO field and they are represented the same way in the database. The difference between them is the way you access the properties that you're inheriting. With OTO: p.user.name; with inheritance: p.name. Making the OTO field of inheritance explicit, you can specify it's properties, like related_name. Commented Sep 10, 2014 at 15:55

1 Answer 1

1

I'm assuming that what you are looking for is handling permissions. A normal django user can do certain things whereas a Parent can do other things. Is that correct?

In that case a better use case would be to use the built-in groups with permissions that django has. You can thus create a group called parents. Which has certain permissions whereas a normal django user can't do those stuff. You can read more about permissions in the docs.

If you are looking for adding more data to the User it would be better to add that to a profile where you have a OneToOneField to the User

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to accomplish both: a Parent has specific capabilities and specific data that a normal django user don't. What you are saying is that I shouldn't use inheritance with the User model?
The way I would solve it is create a profile for the user and then have a baseprofile with class Meta: abstract = True and out of that create a Parent profile. Let me know if you want me to include that in the answer in a clearer way.
Why create a base profile and not a profile via OTO field directly? Do you mean the "before 1.5" way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.