What i want:
Store information about running of group of people.
What i did:
from django.db import models
from django.contrib.auth.models import User
from datetime import timedelta
class Route(models.Model):
name = models.CharField(max_length=50)
class Run(models.Model):
date = models.DateField()
type = models.ForeignKey(Route, on_delete=models.PROTECT)
runners = models.ManyToManyField(User, through='RunnerResult', through_fields=["user", "run"])
class RunnerResult(models.Model):
user = models.ForeignKey(User, on_delete=models.PROTECT)
run = models.ForeignKey('Run', on_delete=models.PROTECT)
result = models.DurationField(default=timedelta())
Problem:
When i do makemigrations i have the following error:
SystemCheckError: System check identified some issues:
ERRORS:
run.Run.runners: (fields.E339) 'RunnerResult.run' is not a foreign key to 'User'.
HINT: Did you mean one of the following foreign keys to 'User': user?
run.Run.runners: (fields.E339) 'RunnerResult.user' is not a foreign key to 'Run'.
HINT: Did you mean one of the following foreign keys to 'Run': run?
Tried to swap through_fields and models between each other and some other actions. I'm starting to think of my misunderstanding of M2M relationship.
You specified the through_fields in the wrong order. You first should specify the relation that refers to the source, and then then one to the target, so:
class Run(models.Model):
date = models.DateField()
type = models.ForeignKey(Route, on_delete=models.PROTECT)
runners = models.ManyToManyField(
User,
through='RunnerResult',
through_fields=('run', 'user')
)
Since there is however only one ForeignKey to Run, and one to User, you do not need to specify the through_fields=… parameter [Django-doc]. So you can implement this as:
class Run(models.Model):
date = models.DateField()
type = models.ForeignKey(Route, on_delete=models.PROTECT)
runners = models.ManyToManyField(
User,
through='RunnerResult'
# no through_fields
)
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Related
I would like to create a bidirectional one-to-many and many-to-one relationship in django like:
class User(models.Model):
device = dont_know_what_to_write()
class Device(models.Model):
user = models.ForeignKey(
User,
on_delete = models.CASCADE
)
What should I do?
In Django if you define a relation from A to B, then Django will automatically add a conceptual relation from B to A which you can query. Django thus already has added the relation in reverse. Indeed, if you implement the models with:
class User(models.Model):
# no device
pass
class Device(models.Model):
user = models.ForeignKey(
User,
on_delete = models.CASCADE
)
then you can access the set of device related to the user with:
myuser.device_set.all()
this is a QuerySet that will contain all Devices that have myuser as user.
You can specify another name with the related_name=… parameter [Django-doc]:
class Device(models.Model):
user = models.ForeignKey(
User,
on_delete = models.CASCADE,
related_name='devices'
)
then you can obtain the devices with:
myuser.devices.all()
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
I would like to execute a single query in Django which retrieves related data, by foreign key, in multiple tables. At present I have to run a query on each table e.g. (House, Furniture, People) using the House number as a filter.
In SQL I can do this in one query like this:
SELECT house.number, house.number_of_rooms, furniture.type, people.name
FROM (house INNER JOIN furniture ON house.number = furniture.house_number)
INNER JOIN people ON house.number = people.house_number
WHERE (((house.number)="21"));
Can this be done in Django?
See example models below:
class House(models.Model):
number = models.CharField('House Number', max_length=10, blank=True, unique=True, primary_key=True)
number_of_rooms = models.IntegerField(default=1, null=True)
class Furniture(models.Model):
house_number = models.ForeignKey(House, on_delete=models.CASCADE, null=True)
type = models.CharField('Furniture Type', max_length=50)
class People(models.Model):
house_number = models.ForeignKey(House, on_delete=models.CASCADE, null=True)
first_name = models.CharField('First Name', max_length=50)
In your models add related_name arguments for foreign keys, so that you can retrieve the objects related to the House() instance.
class Furniture(models.Model):
house_number = models.ForeignKey(House, related_name='house_furniture', on_delete=models.CASCADE, null=True)
type = models.CharField('Furniture Type', max_length=50)
class People(models.Model):
house_number = models.ForeignKey(House, related_name='house_people', on_delete=models.CASCADE, null=True)
first_name = models.CharField('First Name', max_length=50)
Then run the migration using following commands.
python manage.py makemigrations
python manage.py migrate
Then create a new serializers.py module in the same app.
#import models Furniture, People, house
from rest_framework import serializers
class FurnitureSerializer(serializer.ModelSerializer):
class Meta:
model = Furniture
fields = ['type'] # if you want all the fields of model than user '__all__'.
class PeopleSerializer(serializer.ModelSerializer):
class Meta:
model = People
fields = ['first_name'] # if you want all the fields of model than user '__all__'.
class HouseSerializer(serializer.ModelSerializer):
house_furniture = FurnitureSerializer(many=True)
house_people = PeopleSerializer(many=True)
class Meta:
model = Furniture
fields = ['number', 'number_of_rooms', 'house_furniture', 'house_people']
Now, in your views.py you can simply query on model House and serializer the result with HouseSerializer().
#import models from models.py
#import serializer from serializers.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.generics import ListAPIView
class ListHouseView(ListAPIView):
serializer_class = HouseSerializer
queryset = House.objects.filter() #here you can apply filters on the fields of house model and user using related_name you can filter on other related models as well.
Now, simply call ad this in your app's urls.py
url_pattern = [
path('list-house/', ListHouseView.as_view()),
]
Make sure that have a path in your project's urls.py to reach this app's urls.py.
The usual Django way of dealing with this is Queryset.prefetch_related() and iterating through Python (unless you're using Postgres, which has its own solution of ArrayAgg). Given your models, it'll cost three queries, but you won't have to deal with de-normalized row results.
h = House.objects.prefetch_related('furniture_set', 'people_set').get(number='21')
for furniture in house.furniture_set.all():
print(furniture)
for person in house.people_set.all():
print(people)
prefetch_related() caches the results and does the "joining" in Python once the queryset is evaluated, so iterating through the reverse relationships won't incur additional queries, and you're free to structure/serialize the data however you like. The raw SQL from this is something like:
SELECT house.number, house.number_of_rooms FROM house WHERE house.number = '1'
SELECT furniture.id, furniture.house_number_id, furniture.type FROM furniture WHERE furniture.house_number_id IN ('1')
SELECT people.id, people.house_number_id, people.first_name FROM people WHERE people.house_number_id IN ('1')
But Django does that behind-the-scenes so that you can just deal with a model instance in Python.
I'm currently trying to setup some database models in djangos ORM. however im unable to figure out how i'm supposed to reference another models many-to-many- field.
Project model
class Project(models.Model):
projectName = models.CharField(max_length=200)
users = models.ManyToManyField(get_user_model())
projectOwner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='projectowner', default=1)
The users = models.manytomanyfield(get_user_mode()) works fine
and generates the correct relation in the database.
now i want to add a new model that adds a many to many relation between rights and project_user
so what the end result tables are supposed to look like:
project:
projectname - string
projectowner - id of referenced user
user: django orm auth user model
rights:
name
description
etc
project_user:
id
project_id
user_id
rights_projectuser:
id
rights_id
project_user_id
now that last one (rights_projectuser) is what i dont know how to make.
You need to turn "project_user" into a through model that you can then add the many to many relationship to.
class Project(models.Model):
projectName = models.CharField(max_length=200)
users = models.ManyToManyField(get_user_model(), through='ProjectUser')
class ProjectUser(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
rights = models.ManyToManyField(Right)
i now get the following problem when running this code:
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class Project(models.Model):
projectName = models.CharField(max_length=200)
users = models.ManyToManyField(get_user_model(), through='ProjectUser')
projectOwner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='projectowner', default=1)
class Right(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=1000)
class ProjectUser(models.Model):
user_id = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
project_id = models.ForeignKey(Project, on_delete=models.CASCADE)
rights = models.ManyToManyField(Right)
ValueError: Cannot alter field wspingpong.Project.users into wspingpong.Project.users - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
I am working on the following two Django models:
Organisation model which has the User as Foreign key and the Category list which has the Organisation as its Foreign Key.
Following are the Models:
# Create your models here.
from django.contrib.auth.models import User
from django.db import models
class Organisation(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=True
)
organisation_name = models.TextField(
primary_key=True,
blank=True
)
def __str__(self):
return self.organisation_name
class Category(models.Model):
# renamed organisation to organisation_name
organisation_name = models.ForeignKey(
Organisation,
on_delete=models.SET_NULL,
null=True
)
category = models.TextField(
blank=True,
max_length=200
)
class Meta:
verbose_name_plural = 'Category'
Now I have got a huge list of 150+ values stored in my settings.py file which I want to add within the Category model.
The CATEGORY_LIST = ['value', 'value2', ...., 'valueN'] looks like this
This is the script I am executing in shell:
from Venter.models import Organisation, Category
from Backend import settings
cat_list = settings.CATEGORY_LIST # the list is getting loaded into cat_list
org_name = Organisation.objects.get(organisation_name='ABC') # exists
for x in cat_list:
Category.objects.create(organisation=org_name, category=x)
However I am encounter the following error:
django.db.utils.OperationalError: foreign key mismatch - "Mysite_category" referencing "Mysite_organisation"
where: Mysite is my app name in Django project.
(Posted solution on behalf of the question author).
The Python interpreter was incorrectly referencing the 'Organisation' model and not the 'organisation' field of the 'Category' model, it was a naming convention problem. I have now resolved it
Mysite_category has id without PK. Same on second table Mysite_organisation.
CREATE TABLE "Mysite_category" (
"id" integer NOT NULL,
"name" varchar(255) NOT NULL,
"description" text NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
Focus on id and same query will be for second table. This is not important you have to make id as primary key via this query. There are so many options to do that. Make id as PK, it'll be fine.
I have a django model as follows:
class Subscription(models.Model):
Transaction = models.ManyToManyField(Transaction, blank=True, null=True)
User = models.ForeignKey(User)
...etc...
I am trying to add a ManyToMany field to the User model as follows:
SubUsers = models.ManyToManyField(User, blank=True, null=True)
but I get this error when I run syncdb:
AssertionError: ManyToManyField(<django.db.models.fields.related.ForeignKey object at 0x19ddfd0>) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string 'self'
If I encase User in quotes, I get instead:
sales.subscription: 'User' has a relation with model User, which has either not been installed or is abstract.
I know the User model is imported correctly. Any ideas why having 2 fields pointing to the User model causes problems? Thanks in advance...
The reason why it fails is because the name of your field is the same as the class name (User). Use lowercase field names, it the standard convention in Django and Python. See Django Coding style
Also, you need to add a related_nameparameter to your relationship:
class Subscription(models.Model):
user = models.ForeignKey(User)
sub_users = models.ManyToManyField(User, blank=True, null=True, related_name="subscriptions")