I have extended my user model with the OneToOne model and would now like to be able to change additional information via the Wagtail user editing interface.
Customers App
#customer.model
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='customers')
org_title = models.CharField(max_length=500, default='', blank=True)
I have already seen this topic in wagtail docs: https://docs.wagtail.io/en/v2.9.3/advanced_topics/customisation/custom_user_models.html
But AbstractUser models are used. How can this be achieved with related models through the OneToOne relationship?
Related
example field that i want to add in built in User model
class bulit_in_user_model(models.Model):
relationships = models.ManyToManyField('self',on_delete=models.CASCADE)
how to do that
Read more about referencing self using ManyToManyField.symmetrical here
from django.db import models
class MyUser(models.Model):
...
friends = models.ManyToManyField("self", blank=True)
Monkey patching could achieve your goal,but it's better to follow the normal User model Customization though
Inherit from AbstractUser, and specify AUTH_USER_MODEL = 'myapp.MyUser in your settings.
class MyUser(AbstractUser):
friends = models.ManyToManyField("self", blank=True)
I am trying to build an app but have a hard time to figure out my model. I have already setup the User authentication. Next:
Each User needs to be member of a Company (company has address info etc)
Each User should have a Profile
Invoices should be presented based on Company membership so every member gets the same page of invoices.
So the tables (models) are:
Profile
Invoice
Company
User (this already exists and is the default django User)
What would be the appropiate model relationships between those models. I would really appreciate the written model relationships like ForeignKey and ManyToMany e.d.
We may define relations to Company and User in Profile:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
# Other fields here
class Invoice(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
# Other fields here
You may want to have ManyToManyField as the relation to company based on your domain constraints.
I have the following models
from django.contrib.auth.models import User
User = settings.AUTH_USER_MODEL
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
class Tutor(UserProfile):
# tutor_fields here
From User object how can I access Tutor? is it with user.profile?
user.profile.select_related('tutor') does not seem to work.
OneToOneField work like ForeignKey except unique=True and you don't need to specify the related_name (you can specify if you want to change it).
For you example:
from django.contrib.auth.models import User
User = settings.AUTH_USER_MODEL
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
class Tutor(UserProfile):
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name='tutor')
details = models.CharField(max_length=100)
NB: Use a ForeignKey if you want/need one Tutor for many UserProfile, else if you want one (and only one) Tutor for one UserProfile
And you can access to the Tutor bu UserProfile by UserProfile.tutor.details.
As described in the django docs, you should be able to access it with user.user_profile.
I have the following Django model:
class opetest(models.Model):
name = models.CharField(max_length=200)
people = models.ManyToManyField(User, blank=True)
This m2m relationship is available on User object as 'opetest_set'.
How can I make available to edit this m2m relationship in django admin 'User edit page'?
To edit m2m in Django admin use InlineModelAdmin. In order to use your own UserAdmin you should unregister User first, like this:
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
To make opetest accessible from User instance:
class opetest(models.Model):
name = models.CharField(max_length=200)
people = models.ManyToManyField(User, blank=True, related_name='opetests')
Then user.opetests.all()
The site I'm building allows users to create "posts" and has a Twitter-like concept of followed users. For a given user, I'd like to show all of the posts from the users that they follow.
Here are my simplified models:
class User
# a standard django.contrib.auth user model
class UserProfile(models.Model):
# my AUTH_PROFILE_MODULE for django-profiles
user = models.ForeignKey(User, unique=True)
following = models.ManyToManyField('self', symmetrical=False, related_name="followed_by")
class Posts(models.Model):
user = models.ForeignKey(User)
post = models.TextField()
Question: How do you create a queryset of all Post objects from the Users that a given User is following?
I think I've made it more complicated by creating the "follow" relationship on UserProfile, which is not the model with the ForeignKey relationship with Posts.
UPDATE! Here's the answer:
Posts.objects.filter(user__userprofile__in=UserProfile.objects.get(user=your_user_object).following.all())
Posts.objects.filter(user__in=UserProfile.objects.get(user=your_user_object).following)