Django filter ManyToManyField - django

There is a Django filter problem with ManyToManyField that bothers me.
I have a task that I want the creator and selected members of the creator to view. Now I have a problem that the specified member can be viewed normally, but when the creator views it, there will be the same number of duplicate tasks as the specified member. I can't understand this problem, it is not what I expected.
#views
class TaskView(LoginRequiredMixin, View):
def get(self, request):
projecttask_all = ProjectTask.objects.filter(Q(owner=request.user.username) |
Q(task_member=request.user))
print(projecttask_all)
#print results
<QuerySet [<ProjectTask: user1>, <ProjectTask: user1>]>
// My understanding should be like <QuerySet [<ProjectTask: user1>]>, because the owner is not in projecttask_task_member,but it is not.
#model
class ProjectTask(models.Model):
title = models.CharField(max_length=100, verbose_name='title', default='')
owner = models.CharField(max_length=30, verbose_name='owner')
task_member = models.ManyToManyField('UserProfile',related_name='task_member', blank=True, null=True)
#mysql
projecttask
| id | title | owner |
| -- | ----- | ----- |
| 1 | test | user1 |
projecttask_task_member
| id | projecttask_id | userprofile_id |
| -- | -------------- | -------------- |
| 1 | 1 | 8 |
| 2 | 1 | 9 |

Related

Do an INSERT INTO a User table has been "extended" using django.contrib.auth.models import AbstractUser

I've cloned a Django/Angular Application and built it. The data migrations produced an empty database. I'm now attempting to set up a way to login as there is no account setup on the login page. The use of the AbstractUser is supposed to require that an email is required in place of a user name.
Trying to find a way to set up the login.
The User model is:
class User(AbstractUser):
email = None
first_name = None
last_name = None
REQUIRED_FIELDS = []
def __str__(self):
return self.email
class Meta:
db_table = 'User'
Table Description is:
Table "public.User"
Column | Type | Collation | Nullable | Default
--------------+--------------------------+-----------+----------+------------------------------------
id | integer | | not null | nextval('"User_id_seq"'::regclass)
password | character varying(128) | | not null |
last_login | timestamp with time zone | | |
is_superuser | boolean | | not null |
username | character varying(150) | | not null |
is_staff | boolean | | not null |
is_active | boolean | | not null |
date_joined | timestamp with time zone | | not null |
Using this description I've attempted to some add data to facilitate being able to log in.
select * from "User";
id | password | last_login | is_superuser | username | is_staff | is_active | date_joined
----+--------------+------------+--------------+----------+----------+-----------+------------------------
1 | blankDon012! | | t | donfox1 | t | t | 2021-11-12 00:00:00-05
Not able to insert an email address. Articles on internet seem to not quite address this issue so far.

Linking one table to each user in Django

I am working on a Django project where I need to link one table(model) to each user.
Assume MyTable_1 maps to user_1 and so on.
The primary key for MyTable will be a DateField which contains continuous dates from the time user signed-up.
MyTable_1 for User_1
|-----------|----------|-------------|-----------------|
| Date(PK) | food_ate | game_played | ran_today |
|-----------|----------|-------------|-----------------|
| 10/01/20 | rice | chess | Yes |
|-----------|----------|-------------|-----------------|
| 11/01/20 |sandwhich | tennis | No |
|-----------|----------|-------------|-----------------|
MyTable_2 for User_2
|-----------|----------|-------------|-----------------|
| Date(PK) | food_ate | game_played | ran_today |
|-----------|----------|-------------|-----------------|
| 16/03/19 | pizza | rugby | Yes |
|-----------|----------|-------------|-----------------|
| 17/03/19 | pasta | football | Yes |
|-----------|----------|-------------|-----------------|
And so on for every new user created. User logs in those information in MyTable.
How can I implement this? I am using PostgreSQL and have written custom User Model.
You really don't need seperate tables just seperate rows.
A ForeignKey relation will do the trick, something like this in your models.py:
# user model:
User(models.Model, ...):
first_name = models.CharField(...)
last_name = models.CharField(...)
...
# log model:
Log(models.Model):
user = models.ForeignKey(User, ...)
date = models.DateField(...)
food_ate = models.CharField(...)
game_played = models.CharField(...)
ran_today = models.CharField(...)
class Meta: unique_together = ('user', 'date',)
Then, elsewhere, you can access your users' logs like so:
user = User.objects.get(id='the_user_id')
logs = user.logs.all()

Django count distinct number of related model

Consider the following models:
class Publisher(models.Model):
name = models.CharField(max_length=300)
num_awards = models.IntegerField()
class Book(models.Model):
name = models.CharField(max_length=300)
pages = models.IntegerField()
publisher = models.ForeignKey(Publisher, related_name='related_books')
From a Publisher instance how can I get the number of book by distinct value on pages field? For example:
| name | pages | publisher |
|-----------|-------|-----------|
| Golden | 20 | 1 |
| Grey | 23 | 1 |
| Blue | 20 | 1 |
| Grotesque | 27 | 2 |
If I have publisher = Publisher.objects.get(id=1) how can I achieve something like this:
# equals to 2 [Golden, Grey]
publisher.related_books.all().distinct('pages').count()
You were close, you just need to restrict returned values, like so:
publisher.related_books.all().values('pages').distinct('pages').count()
This will just give you the number of different page lengths for a publisher, but not the associated books for each page length. To do that you'd probably need an extra query.
If you want reusable queries, you could do this:
class BookQuerySet(models.QuerySet):
def by_publisher(self, publisher):
return self.filter(publisher=publisher)
def distinct_number_of_pages(self):
return self.distinct(pages)
class Book(...):
...
objects = BookQuerySet.as_manager()
class Publisher(...):
#property
def number_of_page_lengths(self):
return Book.objects.by_publisher(self).distinct_number_of_pages().count()

How to have a many to many relation that enforces uniqueness when I use an intermediate model?

I use intermediate model for "ManyToManyField using the through"
Normally,If I don't use intermediate field, the m2m relation will be unique and can't have the duplicated data.
After I use intermediate model. the relation between m2m can have same data. like this
| | ['0'] (
| | | addToProfile => Array (0)
| | | (
| | | )
| | | endDate = NULL
| | | feedType = "N"
| | | id = 1
| | | info = "Big Kuy No Fear"
| | | likeMaker => Array (3)
| | | (
| | | | ['0'] = "/api/v2/user/2/"
| | | | ['1'] = "/api/v2/user/2/"
| | | | ['2'] = "/api/v2/user/2/"
| | | )
| | | like_count = "3"
I am building a social network. So this is my feed object that has 3 like_counts . But the three of this like come from the same user "/api/v2/user/2/"
I try to add "unique=True" attribute at m2m field but django come up with the error because It doesn't grant the permission to add the "unique" attribute to m2m field at first. Can anyone help me?
Try to use unique_together in your intermediate model.
class M2MModel(models.Model):
field1 = models.ForeignKey(Model1)
field2 = models.ForeignKey(Model2)
class Meta:
unique_together = ('field1', 'field2')
unique_together doesn't work for M2M relationships. More info.
I just finished a feature that quite similar with your requirement but my choice is to use another simple model as an intermediate model.
Here is my code.
class Vote(models.Model):
class Meta:
unique_together = ['content', 'by']
content = models.ForeignKey(Content)
by = models.ForeignKey(User)
In my case I don't see any benefit to implement ManyToManyField.
Update:
I just found from here that Django is not going to make any built-in unique together for ManyToManyField. You have to implement your own validation to make it unique.

How to query most recent record across multiple tables in django

I have two models that look like this:
class Node(models.Model):
user = models.ForeignKey(User, null=False)
name = models.CharField(max_length=50)
class Activation(models.Model):
node = models.ForeignKey(Node, null=False)
active = models.BooleanField(default=False)
datetime = models.DateTimeField(default=datetimeM.datetime.now)
The activation table stores whether a given node is "active" or not. So to figure out whether a node is active, one needs to get the latest activation record for that node.
I'm trying to figure out how to write a django query that returns all active nodes.
Here is some example data
Node Table
id | name
--------------------
0 | andrew
1 | bill
2 | bob
Activation Table
id | nodeId | active | datetime
--------------------
0 | 0 | false | 01-01-2013:00:01:02
1 | 0 | true | 01-02-2013:00:01:02
2 | 0 | false | 01-03-2013:00:01:02
3 | 1 | false | 01-04-2013:00:01:02
4 | 0 | true | 01-05-2013:00:01:02
5 | 1 | true | 01-06-2013:00:01:02
6 | 2 | false | 01-07-2013:00:01:02
So the query would need to return [node0, node1]
class Node(models.Model):
user = models.ForeignKey(User, null=False)
name = models.CharField(max_length=50)
class Activation(models.Model):
node = models.ForeignKey(Node, null=False, related_name='activations')
active = models.BooleanField(default=False)
datetime = models.DateTimeField(default=datetimeM.datetime.now)
#latest activation record for that node:
try:
latest_activation = node.activations.latest('id')
except:
latest_activation = None
# Return all active notes:
all_active_notes = Node.objects.filter(activations__active=True)
Updated:
Check this question what I posted yestarday:
Django reverse query by the last created object
maybe this will help you.