I read the documentation about many-to-many relationships and the examples. What I could not find is a hint on where to put the ManyToManyField. In my case I have an extended user model Client and a model Pizza. Every client may mark one or more pizzas as favourites. Those are my two models:
from django.db import models
from django.contrib.auth.models import User
class Client(models.Model):
user = models.OneToOneField(User)
#? favourite_pizza = models.ManyToManyField()
class Pizza(models.Model):
name = models.CharField(max_length=255)
#? favourite_pizza = models.ManyToManyField()
In what model should I add the ManyToManyField? Does it matter?
PS The important information is how many favourite pizzas a client has (and which). It is less important how many clients marked a pizza as a favourite (and who). Consequently I would chose to put the ManyToManyField in the Client class.
From the Django documentation:
Generally, ManyToManyField instances should go in the object that’s going to be edited on a form.
Technically it does not matter. The question is from which model-side you will query the database.
Related
I am not sure how I am suppose to represent my relationships correctly. Let's say I have 2 tables User and Post. For this example I will mix and match relationships between these 2 tables.
relationships
1st case
A user has zero-to-many posts and a post belongs to one user.
I looked at the Many-to-One relationships on Django documentation and decided I can maybe model this by doing the following...
class User(models.Model):
pass
class Post(models.Model):
user = models.ForeignKey(User)
I read it as a User can have many post (zero or more).
A Post belongs to one (and only one) user.
2nd case
A User has one-to-many posts and a Post belongs to one user
This is almost identical to the first case but the difference here is that a User has one-to-many post vs. zero-to-many. I'm not sure how to model this. How do I tell my model User to own at least 1 or more posts but not zero.
I may add more of the other relationships to this question if I need further clarification but for now would really like to know how this is suppose to work. The only thing I can think of is having a null=True, blank=True so that I am allowed to have nothing zero-to-many or leave the default so that I have one-to-many
I think the following approach is so popular one
class User(models.Model):
pass
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
this will guarantee no posts with out any user-related
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)
Please am really need you guys help on how to use Django to create Multi-user Account.
e.g Student, Lecturers, and Department login page(Admin).
in this system, the department will be the Admin to register the lecturers in order to have access and why the Student register on their own.
Am design a project titled Online Assignment Submission System
(it is my final year Project). I really need you guys help on how to go about it using Django.
There are multiple options to deal with the user model in Django:
You could subclass AbstractUser or AbstractBaseUser and add a choice field if the user is a student, teacher, etc.
Linking back from a related 'Profile' model. This comes in handy if you want different types of users to have different fields.
models.py:
from django.conf import settings
from django.db import models
class StudentProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
subjects = models.ManyToManyField(Subject)
has_returned_books = models.BooleanField(default=False)
class TeacherProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
salary = models.IntegerField()
hours_per_week = models.IntegerField()
# ... other profiles for department, etc.
Using this approach, you can query fields with the ORM:
user.teacherprofile.salary or user.studentprofile.subjects.
When you have implemented your models you should read about the permission system in Django. You can limit access to logged-in users with help of a decorator or mixin.
Do some more research and come back asking specific question with examples of your recent work. The more work you put into the question, the more people will help you. Good luck!
Stupid question time. In Django, if you want to specify the name of a model's table, you'll do this:
class MyModel(models.Model):
...
class Meta:
db_table = 'mymodel'
Is it possible to change the name of Django's default auth_user table in a similar fashion? I'm always running manual queries against the user table and it would be much faster to type 'user' than 'auth_user'. I've looked at the Django docs and done some Internet searches and I haven't seen anyone address this question.
Thanks!
Both Selcuk and Daniel Roseman have given you good answers. You can extend either one of the classes User or AbstractUser, with the small difference that for the latter you will also need to add the swappable attribute in you Meta class.
Here is an example:
from django.contrib.auth.models import AbstractUser # (or simply User)
class MyUser(AbstractUser):
class Meta:
swappable = 'AUTH_USER_MODEL' # ONLY for AbstractUser
db_table = 'user'
However, pay attention to using 'user' as a table name, because there are databases where this is a reserved word !!!
Good luck.
models:
class Book(models.Model):
name = models.Char()
class BookCount(models.Model):
book = OneToOneField(Book)
count = SmallIntegerField(default=0)
views:
class BookCreate(CreateView):
model = Application
The question is, after create Book, I want insert a record to BookCount. Any ideas?
If your BookCount model is required you could use a listener for the post_save signal along the lines of this:
# models.py
from django.db.models.signals import post_save
# Model definitions
...
def create_book_count(sender, instance, created, **kwargs):
if created:
BookCount.objects.create(book=instance)
post_save.connect(create_book_count, sender=Book)
If your models are really this simple, you may want to drop the BookCount model and add a count field to your Book model instead to reduce the complexity and overhead here. See the docs on extending the user model for a short overview of why it might be better to avoid the OneToOneField option (the wording is specific to the User model, but it applies here, too):
Note that using related models results in additional queries or joins to retrieve the related data, and depending on your needs substituting the User model and adding the related fields may be your better option. However existing links to the default User model within your project’s apps may justify the extra database load.