Django admin inline to model's related model's related model - django

I have two models that have foreign keys to the User table.
class Device(models.Model):
user = models.ForeignKey(User)
class Client(models.Model):
user = models.ForeignKey(User)
So, is it possible to show an Inline form in the Client's admin that shows the Devices that are related to the Client's User?

Related

Additional fields in Wagtail User Edit

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?

Django model with User, Profile and company

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.

how to attach multiple ModelAdmins to UserAdmin in Django?

I have a search model which has a ForeignKey relation to User
class Searches(models.Model):
user = models.ForeignKey(User)
......
I have a UserProfile model which has a OnetoOne Relationship to User
class UserProfile(models.Model):
user = models.OneToOneField(User)
photo = models.ImageField(upload_to='profile_images', blank=True)
ispublic=models.NullBooleanField()
I have attached UserProfile in admin.py as follows:
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
class UserProfileAdmin(UserAdmin):
inlines=(UserProfileInline, )
list_filter = UserAdmin.list_filter + ('email',)
list_display=('username','email','first_name','last_name','isPublic')
admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), UserProfileAdmin)
Now I do not see a separate UserProfile but is integrated into User, which is what I want.
I also want to have Search model to show up in User admin. But also seperately.
how can I register two (or more) Admins to User model?
Try just putting another Inline inside the UserProfileAdmin, that will then place the UserProfileInline and SearchesInline in the UserProfileAdmin, then put admin.site.register(Searches) in admin.py. Unless I misunderstand the question.

Register different model fields in another model's admin through foreign key

Is it possible to add instructors Album in user's admin page?
/instructors
model.py
class Album(models.Model):
instructor = models.ForeignKey(Instructor, related_name='albums')
/user
model.py
class User(AbstractUser):
...
admin.py
admin.site.register(Album)

Get all objects defined by a Django ManyToManyField relationship

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)