Optimizing ManyToMany (ToMany) Django query - django

I still have a lot to learn about Django models, so be gentle.
Consider this model:
from django.db import models
from django.contrib.auth.models import User
class Community(models.Model):
users = models.ManyToManyField(User)
class Portal(models.Model):
community = models.ForeignKey(Community, blank=True, null=True)
class Page(models.Model):
portals = models.ManyToManyField(Portal, null=True)
So a Page can be in many Portals. Each Portal has a Community and a Community has many users.
Now I'm trying to find all the users related to a single page:
def allowed_users(self):
return User.objects.filter(community__in=Community.objects.filter(portal__in=self.portals.all()))
This works, but I'm sure there is a more efficient way of doing it. Possibly with Q or F.
Any help is appreciated.

You can use the double-underscore syntax to traverse relationships. Assuming allowed_users is a method on Page:
User.objects.filter(community__portal__page=self)

Related

I want to add "favourite books or basket" in django models

I'm trying to understand relationships on django and create a little project but I'm stuck.So basically I want to add a field (sort of basket) so that people can add their favourite items among many of them. But I can't implement it. What should I do inside my models? Thanks. Bye the way I want to turn this into RESTFUL.I'll appreciate if you give me any advice.
models.py
from django.db import models
from django.contrib.auth.models import User
class Products(models.Model):
name = models.CharField(max_length=20)
price = models.IntegerField()
fav_product = models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.name
It should be ManytoMany relationship in my opinion. Product can has many users (many users can add the product to favourites) and user can has many products (user can add many products to favourites).

Django User-Interests App and its database Model design

I'm am building a django app which takes user Interests as inputs.
Now I have 2 Questions -
First is that, what model should I use, should I just add a field to user model or a separate Interest Model and link via Foreign Key?
I know the former design is bad, and so I.m trying latter one, I'm having a hard time in Django to create Interest Model and its view to save the user interests.
Any help is appreciated.
I am trying to accomplish the same thing.
Here is how I have solved it:
I have not tried it out yet, but this should work as a solution.
from django.contrib.auth.models import User
class Nation(models.Model):
name=models.CharField(max_length=64)
class Subject(models.Model):
name=models.CharField(max_length=64)
class Interests(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nationals = models.ManyToManyField(Nation)
subjects = models.ManyToManyField(Subject)

Django Multiple User types with a payed system

I am extremely new to the Django frame work and needed a little more insight. I am currently building a project to have two user types.Example would be a teacher and student type application. The main difference would be that I would like to have one of the users to have a payed tier.
Example: I would love to have it so that the teacher can only log in if payed.
My other question would be.
I already have been working on the project before coming to realize the AbstractUser might be a better solution.
Can I get around using AbstractUser with using OnetoOneModels with this type of setup?
I found documentation saying it's best to use AbstractUser before any migrations are made, how would I go about changing my project to suit these new requirements "If AbstractUser is best"? If there is no easy way around it I'm not above starting over to make it right.
Andrew
You can inherit your custom user model from AbstractUser and add in it your custom fields.
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractBaseUser):
...
date_of_birth = models.DateField()
height = models.FloatField()
For example for user types you can add foreign key field:
user_type = models.ForeignKey(
UserType,
on_delete=models.SET_NULL
)
Or you can add some boolean field:
is_paid_tier_user = models.BooleanField(default=True)

Django 2.1 Models Importing Custom User Model

I have been working on an extended User model in my Django 2.1 project. I am curious to know if the way in which I am importing my CustomUser model into another model (for use as a ForeinKey) is the correct way of doing so.
I have encountered verbiage in the past indicating that it is not correct to simple import the User model from the admin app, but rather import it from django.conf.
example importing from the base User model:
from django.conf import settings
User = settings.AUTH_USER_MODEL
...
class <ModelName>(models.mode):
user = ForeignKey(User, on_delete=models.CASCADE, default=1)
Now that I am using a CustomUser Model (extending AbstractUser),
users/models.py:
class CustomUser(AbstractUser):
objects = CustomUserManager()
def __str__(self):
return self.username
Is it better practice to import this model via setting (as shown above) or is how I am doing it below (in my Post app) the right way to it:
posts/models.py:
from users.models import CustomUser
class Post(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, default=1)
I am assuming this is the wrong way to go about this, but I am not sure why, can someone let me know why the above is not best practice? Note: It does get the job done though.
Thanks!
There's nothing wrong with the way you are importing. The "settings" way of importing is merely a round-about way to get to the underlying model, since Django allows you to use a custom Model for user authentication handling.
Even better, however, is using the "lazy" load approach, which doesn't require any import statements at all:
user = models.ForeignKey('CustomUser', on_delete=models.CASCADE, default=1)
Be careful using a default on a ForeignKeyField, by the way. You need to be absolutely certain that the default value you provide will already be present, and will never disappear from the database.

Django app structutre and circular reference

I'm trying to keep my project well organized, so I try to keep it splitted to apps.
Assume a blog app with a BlogPost model.
Now I add to that a Tag app, which has a Tag model with foreign key to Post.
Now if I want to write a method get_tags(), in the Blog class, that would be circular reference.
So is that a bad design? Maybe I should not write such method on the blog, or such related models should simply be in the same app?
I'm Simply trying to learn how to organize my (big) project. I've read a lot about django app concept, stil haven't found a right way
The point here is that Django automatically creates reverse lookup when you create a ForeignKey or ManytoManyField. Assuming your models are as follows:
BlogPost Model
from django.db import models
class BlogPost(models.Model):
title = models.CharField(_('title'), max_length=200)
slug = models.SlugField(_('slug'), unique_for_date='publish')
author = models.ForeignKey(User, blank=True, null=True)
body = models.TextField(_('body'), )
publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
created = models.DateTimeField(_('created'), auto_now_add=True)
Tag Model
from django.db import models
from Blog.models import BlogPost
class Tag(models.Model):
Post = models.ForeignKey(BlogPost,related_name="tags")
Now, assuming you are generating the Tags of a post in a view, you can basically get all the tags of a post by just calling blogpost.tags_set where blogpost is a model instance of BlogPost.