Supposing a large number of users and a large number of videos, what would be a better way to structure the database of the following two options:
Option one --
class UserProfile(models.Model)
user = models.ForeignKey(User, unique=True)
videos = models.ManyToManyField(Video, blank=True)
class Video(models.Model)
title = models.CharField(max_length=256)
file = models.FileField(...)
Option two --
class UserProfile(models.Model)
user = models.ForeignKey(User, unique=True)
class Video(models.Model)
uploaded_by = models.ForeignKey(User)
title = models.CharField(max_length=256)
file = models.FileField(...)
Videos have nothing to do with user profiles, so the first relation is fallacious. It will also require spanning another table in order to find a user's videos. Use the second.
Related
I have 2 model classes in Django:
class Notification(models.Model):
receiver = models.ForeignKey(User, null=True, blank=True)
content = models.CharField(max_length=500)
object_id = models.IntegerField(blank=True, null=True)
type = models.TextField(max_length=200, blank=True, null=True)
Class Notification stores notification about users activity. Field "content" is like: "welcome you registered Business Course successfully", or "5ASC is your voucher code". Field type stores types of object: course, promotion.
class PaymentTransaction(models.Model):
course = models.ForeignKey(Course)
student = models.ForeignKey(User)
PAYMENT_STATUS = ( SUCCESS, FAILURE, PROCESSING)
payment_status = models.CharField(max_length=50, choices=PAYMENT_STATUS, default=PROCESSING)
In notification pop up, when he clicks to paid Course then go to Course detail page and start learning, when he clicks to unpaid Course then go to Course register page, when he clicks to promotion code then go to promotion code page
How to have a QuerySet return all fields of Notification and PaymentTransaction tables, and condition is Notification.receiver_id = PaymentTransaction.student_id .
For each Course notification, i want to get Course payment status.I did:
user = request.user
p_list = PaymentTransaction.objects.filter(student=user)
n_list = Notification.objects.filter(receiver=user).intersection(p_list)
But it did't work
I can't understand why you create the Models like this but:
I think it should be:
class Book:
title = models.CharField(max_length=500)
price = models.FloatField()
class User:
name= models.CharField(max_length=500)
something = models.CharField()
class Book_User:
user = models.ForeignKey(User)
book = models.ForeignKey(User)
detail = models.CharField()
And i what is noti for?Just show up the list?
~> it should be the list of Book_User in page of user
~> Problem solve
I'm trying to relate Persons, their Skills and Skill levels. My model looks like this:
class Person(models.Model):
first_name = models.CharField(max_length=200)
middle_name = models.CharField(max_length=200, blank=True)
last_name = models.CharField(max_length=200)
skills = models.ManyToManyField(Skill)
and
class Skill(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=1000, blank=True)
active = models.BooleanField(default=True)
It does work (in admin) and I'm able to create a Person, add several skills etc. However, I also need the skill level information for each Skill related to each Person. Something like this:
BASIC = 'BAS'
NOVICE = 'NOV'
INTERMEDIATE = 'INT'
ADVANCED = 'ADV'
EXPERT = 'EXP'
SKILL_LEVEL_CHOICES = (
(BASIC, 'Basic knowledge'),
(NOVICE, 'Novice (Limited experience)'),
(INTERMEDIATE, 'Intermediate (Practical application)'),
(ADVANCED, 'Advanced knowledge'),
(EXPERT, 'Expert'),
)
I'm not sure what should I add to Person to have information for each skill and its level.
Thanks.
After gathering a little bit more experience on Django, I realized that it offers pretty simple solution to my problem :)
I needed two classes, SkillLevel and SkillWithSkillLevel.
class SkillLevel(models.Model):
name = models.CharField(max_length=20)
description = models.CharField(max_length=100, blank=True)
class SkillWithSkillLevel(models.Model):
skill = models.ForeignKey(Skill)
level = models.ForeignKey(SkillLevel)
person = models.ForeignKey(Person)
That's it.
It helps to have a little bit customized admin:
class SkillWithSkillLevelInline(admin.TabularInline):
model = SkillWithSkillLevel
extra = 3
And then, of course, register SkillWithSkillLevelInline within PersonAdmin, inlines = [SkillWithSkillLevelInline]
Person obviously doesn't need skills = models.ManyToManyField(Skill).
Django noob questions:
I want to create a site which allows users to share info about cars. Each car should have a collection of images, and the submitter should select one of the images to be used to represent the car on a listing page. A basic set of models is shown below:
class Manufacturer(models.Model):
name = models.CharField(max_length=255)
class ModelBrand(models.Model):
name = models.CharField(max_length=255)
class Car(models.Model):
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True, editable=False)
# identifying information
manufacturer = models.ForeignKey(Manufacturer)
model_brand = models.ForeignKey(ModelBrand)
model_year = models.PositiveIntegerField()
class CarImage(models.Model):
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True, editable=False)
car = models.ForeignKey(Car, related_name='images')
source_url = models.CharField(max_length=255, blank=True)
image = ImageField(upload_to='cars')
But how do I model the selected image? Do I put a 'selected' BooleanField on the CarImage class? And how do I configure the Car and CarImage admin classes to allow an admin site user to select and image for a car from its 'images' collection?
First, I would like to suggest you to refactor your class using an auxiliary TimeStampedClass
class TimeStampedModel(models.Model):
"""
Abstract class model that saves timestamp of creation and updating of a model.
Each model used in the project has to subclass this class.
"""
created_at = models.DateTimeField(auto_now_add=True, editable=False)
updated_at = models.DateTimeField(auto_now=True, editable=False)
class Meta:
abstract = True
ordering = ('-created_on',)
So you can use this class over your project, subclassing it.
One simple solution for your question is attach your image gallery to your car, and create one attribute that is a IntegerField that stores the picture position in the image gallery:
...
class CarImage(TimeStampedField):
source_url = models.CharField(max_length=255, blank=True)
image = ImageField(upload_to='cars')
class Car(TimeStampedModel):
image_gallery = models.ManyToManyField(CarImage)
selected_picture = models.IntegerField(default=0)
# identifying information
manufacturer = models.ForeignKey(Manufacturer)
model_brand = models.ForeignKey(ModelBrand)
model_year = models.PositiveIntegerField()
So, if selected_picture is n, you just need to get n-th picture inside image_gallery
My site will have news with possibility to rate them and leave comments. Each user will be able to rate one news only once and leave only one comment. At the same time, I should know which user rated the news and who left the comment.
How to organize such database?
I think about the following structure:
class News(db.Model):
news = db.TextProperty()
added = db.DateTimeProperty(auto_now_add=True)
rating = db.ReferenceProperty(NewsRatings)
comments = db.ReferenceProperty(NewsComments)
added = db.DateTimeProperty(auto_now_add=True)
class NewsRatings(db.Model):
user = db.ReferenceProperty(Users)
rating = db.IntegerProperty()
added = db.DateTimeProperty(auto_now_add=True)
class NewsComments(db.Model):
user = db.ReferenceProperty(Users)
comment = db.TextProperty()
added = db.DateTimeProperty(auto_now_add=True)
class Users(db.Model):
user = db.IntegerProperty()
Is it correct approach? Will I know who left particular comment for particular news?
Your current model only allows for each news item to have a single rating and a single comment (each of which could belong to an arbitrary number of news items). Instead, put the ReferenceProperty on NewsRatings and NewsComments, referencing the News item to which they belong.
Hi I have a lot of users in my system who are classified into different types. I want to store the address details of all those users. For instance the user could be a student, a school or a franchisee. All the users here could have an address information associated with them.
from django.db import models
from django.contrib.auth.Models import User
class Address(models.Model):
user = models.OneToOneField(User)
address = models.TextField()
class Student(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
class School(models.Model):
user_id = models.ForeignKey(User)
address = models.ForeignKey(Address)
contact_person_name = models.CharField(max_length=50)
In this scenario there are 2 references to the User model - one through user_id and another through address.user though they should be referring to the same instance. Is it a bad practice to have duplicate references?
I thought of leaving out the 'user' foreignkey in Address, but I think that the address can't exist without a user. How to better model this?
As you already mentioned in question duplication of same field in
a model is not a good Idea.
If these are your actual models, I would suggest you using abstract
models:
from django.db import models
from django.contrib.auth.Models import User
class Profile(models.Model):
user = models.OneToOneField(User, related_name="%(app_label)s_%(class)s_related")
address = models.TextField()
class Meta:
abstract = True
class Student(Profile):
pass
class School(Profile):
contact_person_name = models.CharField(max_length=50)
This will generate two tables: students, schools with fields
user, address and user, address, contact_person_name
respectively.