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)
Related
Django Model and Form that I want to make
Hello. I'm fairly new to Django and I have a hard time understanding which model field to use. I didn't post any code because my code is so messy right now and most of it still following "Django for Beginner" book by William S. Vincent.
I have an app consisting 2 models, Product model and Production model. Some fields on production model are linked to product model. Then from production model, I make a form for user input using ModelForm. (There's a link to an image to help understanding model and form relation.)
I have several questions regarding this matter.
If I set product_id on Production Model as ForeignKey to Product, what field should I use for product_name and material?
When I use CharField for both product_name and material, there are entry for both product_name and material on form and I don't want that. How to change it to readonly and have value updated based on product_id? (I'm not sure if this is related to Django form or not.)
Right now I'm using ModelForm to make Production form and then using FormView to render the form. Is it the right approach? What is the difference between this one and CreateView from model?
Thank you in advance for any comments and answers.
If you have a name and a material on the product model, you don't need those on the production model unless they relate to the production object. If I were you, I'd have a foreign key on Production to the product. It might look something like;
class Production(models.Model):
product = models.ForeignKey(
to=Product,
verbose_name=_("Product"),
on_delete=models.CASCADE
)
machine = models.CharField(
verbose_name=_("Machine No"),
max_length=255
)
date = models.DateTimeField(
verbose_name=_("Date"),
blank=True,
null=True
)
Then your form might be;
class ProductionForm(forms.ModelForm):
class Meta:
model = Production
fields = (
'product',
'machine',
'date',
)
I would recommend using the django admin to get your models as you want them before you start working with views. If you know the data is being stored in a way you need, then you can worry about building the frontend. The admin is around page 70 of that book you've got. You can also do readonly fields with that.
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)
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!
My case is related to a purchase, the customer who buys something and the seller who sold it.
Models.py
from django.contrib.auth.models import User
class buy(models.Model):
customer = models.ForeignKey(User)
seller = models.ForeignKey(User)
I am aware that the above code is wrong, I write it that way so the question is understood.
I take the django.contrib authentication system, to avoid having to make another authentication system for clients and one for sellers, I want django code reuse.
A solution had thought of creating another data model to sellers or customers, but in my view and in the login I'm using django.contrib, then I would still use this system authentication would like to know if there is any way or if I ultimately that create another authentication system?
I'm just guessing, if you have a Product model that has a user field in which case he's the actual seller, why don't you use seller = models.ForeignKey(Product, to_field='user')
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.