Good day. I'm trying to write an app which will ask a car seller to register himself and his shop name along with a password. On the other side, I would like a buy, to register himself using his a username and password.
The user will be able to login, chose a car model catalogue and then can buy.
But I'm facing an issue with the models design.
Here are my models:
class ShopOwner(AbstractUser):
def __str__(self):
return self.username
class Buyer(AbstractUser):
def __str__(self):
return self.username
class Shop(models.Model):
owner = models.ForeignKey(ShopOwner)
name = models.CharField(max_length=20)
def __str__(self):
return self.name
Class Catalogue(models.Model):
shop = models.ManyToManyField(Shop)
name = models.CharField(max_length=20)
def __str__(self):
return self.name
class Car(models.Model):
catalogue = ManyToManyField(Catalogue)
name = models.CharField(max_length=20)
price = models.DecimalField(max_digits=100, decimal_places=2)
def __str__(self):
return self.name
I will appreciate any advise on this.
Related
I have three models as follows.
class Quiz(models.Model):
name = models.CharField(max_length=50)
desc = models.CharField(max_length=500)
number_of_questions = models.IntegerField(default=1)
time = models.IntegerField(help_text="Duration of the quiz in seconds", default="1")
def __str__(self):
return self.name
def get_questions(self):
return self.question_set.all()
class Question(models.Model):
ques = models.CharField(max_length=200)
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
def __str__(self):
return self.ques
class Answer(models.Model):
content = models.CharField(max_length=200)
correct = models.BooleanField(default=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return f"question: {self.question.ques }, answer: {self.content}, correct: {self.correct}"
I have to create api for create question. for this I have to use model Question and Answer same time e.g.
its admin page but I want to create same api which accept above parameters and store it into database.
how to write view.py for this api ?
I am new to data modeling and trying to develop a Django model for a simple food tracker.
What did you eat?
Pancakes
Meal
Breakfast
Ingredients
Eggs
Flour
Milk
Allergens
Eggs
When the user logged their Food, Meal, and Ingredients it would lookup to see if any of those ingredients are a known allergen. What would be the appropriate data model for this? My guess is below.
from django.db import models
from django.conf import settings
class Meal(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Food(models.Model):
date = models.DateTimeField()
name = models.CharField(max_length=50)
notes = models.TextField(max_length=200, null=True)
meal = models.ForeignKey(Meal, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Ingredient(models.Model):
name = models.CharField(max_length=50)
foods = models.ManyToManyField(Food)
def __str__(self):
return self.name
class Allergen(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
ingredients = models.ManyToManyField(Ingredient)
Just add an is_allergent boolean field to Ingrediant model.
Think of allergy as just an attribute of Ingrediant. it could be allergic or not.
I need to filter the a particular user's bar in whcih reservations were made. I am a beginner in Django and have tried some methods which unfortunately didn't work.
models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True)
class Bar(models.Model):
user_id = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Tables(models.Model):
table_no = models.CharField(max_length=14, unique=False)
bar = models.ForeignKey(to=Bar, on_delete=models.CASCADE)
def __str__(self):
return self.table_no
class Reservation(models.Model):
first_name = models.CharField(max_length=200)
table = models.ForeignKey(Tables, on_delete=models.CASCADE)
def __str__(self):
return self.first_name
Note: I wan to filter the reservations made in a particular user's bar
I know you might be having a hard time and its one the things you go through when you are learning..
1.Can you change this first
from
class Bar(models.Model):
user_id = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __str__(self):
return self.name
To
class Bar(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50, null=True)
def __str__(self):
return self.name
2.On querying your data can you can try this since our user will already on the bar
user_bar = Bar.objects.filter("Here you specify what you wanna filter by")
Or
user_bar = Bar.objects.get(id=pk)
When I used this query filter. it worked perfectly.
reservations = Reservation.objects.filter(table__bar__user_id=request.user)
subject title: People can come to the restaurant and book a reservation filling a form which uses the Reservation model. Then the reservation is connected to a Table which is connected to a bar and the owner of the bar. Now I need to be able to filter the reservations made in each bar owner restaurant.
I'm developing a project in Django. I have several registered companies, and all models are based on the company.
#models.py
class Company(models.Model):
name = models.CharField(max_length=100)
country = models.CharField(max_length=100)
class XUser(User):
phone = models.CharField(max_length=20, null=True, blank=True)
card = models.CharField(max_length=20, null=False, blank=True)
company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.nombre
class Book(models.Model):
user = models.ForeignKey(XUser, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
class Paragraph(models.Model):
company = models.ForeignKey(Company, on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
text = models.CharField(max_length=300)
Now in my admin I define that every user can only see the books of his company.
#admin.py
#admin.register(Book)
class BookAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
# return a filtered queryset
return qs.filter(company=request.user.company)
My question is this: When I try to create a Paragraph instance in the django management form, it shows me all the books and I want it to only show me the ones that belong to the user's Company. Any ideas?
I think you can achieve that by overriding the get_form method to change the queryset of company:
admin.py:
#admin.register(Paragraph)
class ParagraphAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj=None, **kwargs)
form.base_fields['book'].queryset = Book.objects.filter(company=request.user.company)
return form
I have these models: Users, Grades, Groups, Projects, Tasks.
Every Grade, has Users (that can be student or teacher). The teacher can create groups of students. How do I create a ManyToManyField that connects to the specific users of that Grade?
Right Now when I try to make a group, it inherits from Grades and I get EK17A, TE17A (Note that i want to get the users in EK17A, or the users in EK17A)
This is so the teacher can put the students into project groups :D
from django.db import models
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
age = models.IntegerField()
description = models.CharField(max_length=300)
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
class Meta:
verbose_name_plural = 'User Profiles'
def __str__(self):
return self.user.username
#receiver(post_save, sender=User)
def create_user_data(sender, update_fields, created, instance, **kwargs):
if created:
user = instance
profile = UserProfile.objects.create(user=user, is_teacher=False, is_student=True, age=18, description='No Description')
class Grades(models.Model):
name = models.CharField(max_length=10)
members = models.ManyToManyField(UserProfile, blank=True)
class Meta:
verbose_name_plural = 'Grades'
def __str__(self):
return self.name
class TeacherProjectTask(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000, blank=True)
state = models.CharField(max_length=20)
class Meta:
verbose_name_plural = 'Tasks'
def __str__(self):
return self.title
class TeacherProject(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000)
tasks = models.ManyToManyField(TeacherProjectTask, blank=True)
grade = models.OneToOneField(Grades, blank=True, on_delete=models.CASCADE)
class Meta:
verbose_name_plural = 'Projects'
def __str__(self):
return self.title
class ProjectGroup(models.Model):
title = models.CharField(max_length=150)
description = models.CharField(max_length=1000)
members = models.ManyToManyField(Grades, blank=True)
class Meta:
verbose_name_plural = 'Group'
def __str__(self):
return self.title