What is the relation of these 3 models: user, birthday, label? - django

I'm creating a birthday calendar app. It will have the following models:
- Django's user model
- Birthday
- Label (i.e. friends, family)
I'm not sure how to model the Label class. The user should have the ability to create a new Label object which can then be used when creating a Birthday object.
The user should also be able to filter Birthday's based on the Label.
How would you model these relationships in a db?
from django.conf import settings
from django.db import models
from django.utils import timezone
class Birthday(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=25)
day = models.DateField()
class Label(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
tag = models.CharField(max_length=25)

class Label(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
tag = models.CharField(max_length=25)
class Birthday(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=25)
day = models.DateField()
label = models.ForeignKey(Label, on_delete=models.CASCADE)
Use Label as the Foreignkey of Birthday. So you will know which birthday is related to which Label.
You can then filter out Birthday based on label like this.
Birthday.objects.filter(label__id=1)

As I can understand, you can add Label as ManyToMany Field in Birthday Model. In that way, you can assign this label/labels to Birthday. You can have multiple label to a birthday, and one label can have multiple birthdays. So it will be like this:
class Birthday(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=25)
day = models.DateField()
labels = models.ManyToManyField(Label)
Usage:
birthday = Birthday.objects.create(...)
label = Label.objects.create(...)
b.labels.add(label)
Birthday.objects.filter(labels=label)

Related

How To Implement Dynamic Columns in Django models based on POST request

I have created a Django RestFramework with few models like Offers, Dailysubs and Requisitions to which I need add columns dynamically for a POST request.
There should be 1-10 blank columns for a model and a Master table which has fields to configure the new column like name and other properties.
I also need to implement an approach to add these dynamic columns into serializers.
class Requisition(models.Model):
skill = models.CharField(max_length=255)
superClass = models.CharField(max_length=255)
jobFamily = models.CharField(max_length=255)
yearOfExp = models.CharField(max_length=255)
grade = models.CharField(max_length=100)
...
...
class Offer(models.Model):
businessGroup = models.CharField(max_length=100)
status = models.CharField(max_length=20)
sNo = models.IntegerField()
recruiter = models.CharField(max_length=100)
manager = models.CharField(max_length=100)
dateOfOffer = models.DateField()
dateOfJoining = models.DateField()
...
...
class MasterTable(models.Model):
columnName = models.CharField(max_length=100)
tableName = models.CharField(max_length=100)
columnFieldType = models.CharField(max_length=100)
I read another post related to this at Django Models (dynamic?) but I don't know if I should create another model to hold these blank columns or if I should add blank columns to each model.

How to create relative entry in mongoldb using Django?

I have following Person document model.
from djongo import models
from django.contrib.postgres.fields import ArrayField
class Person(models.Model):
id = models.ObjectIdField()
name = models.CharField(max_length=255)
city = models.CharField(max_length=255)
status = models.CharField(max_length=20)
phone_number = models.CharField(max_length=20)
objects = models.DjongoManager()
And the following Comment model
class Comment:
id = models.OneToOneField(
Person,
on_delete=models.CASCADE,
primary_key=True,
)
comments = ArrayField(models.CharField())
objects = models.DjongoManager()
When I create a document in Person object, I want mongo automatically create a document in Comment model with the same id but empty comments. I thought declaring OneToOneField filed will do it, but it didn't. What should I do?

Django Sum in Annotate

Good afternoon,
I am really struggling with getting a sum using Annotate in DJango.
I am using User object and the following models:
class Depts(models.Model):
dept_name = models.CharField(max_length=55)
dept_description = models.CharField(max_length=255)
isBranch = models.BooleanField(default=False)
def __str__(self):
return "{}".format(self.dept_name)
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
title = models.CharField(max_length=75)
dept = models.ForeignKey(Depts, on_delete=models.CASCADE, related_name="dept", null=True)
class ActivityLog(models.Model):
activity_datetime = models.DateTimeField(default=timezone.now)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='activity_user')
activity_category = models.ForeignKey(ActivityCategory, on_delete=models.CASCADE, null=True, related_name='activity_cat')
activity_description = models.CharField(max_length=100, default="Misc Activity")
class ActivityCategory(models.Model):
activity_name = models.CharField(max_length=40)
activity_description = models.CharField(max_length=150)
pts = models.IntegerField()
def __str__(self):
return '%s' % (self.activity_name)
What I need to do is get a group of departments with aggregating the sum of the pts earned by all the users activitylogs.
So a user is part of department, they do activities, each activity is of a type activity_category and has associated points. How can I query using the ORM to get a sum of points for everyone in each department?
Thank you, I cannot seem to wrap my mind around it.
You annotate the departments with the sum:
from django.db.models import Sum
Depts.objects.annotate(
total_pts=Sum('dept__user__activity_user__activity_category__pts')
)
Note: The related_name=… parameter [Django-doc]
is the name of the relation in reverse, so from the Depts model to the UserProfile
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming the dept relation to userprofiles.
After setting the related_name='userprofiles', the query is:
from django.db.models import Sum
Depts.objects.annotate(
total_pts=Sum('userprofiles__user__activity_user__activity_category__pts')
)

What is the process that you follow to create model in Django?

What is the process that you follow to create model in Django? Thanks.
The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the models API like clean, save, or delete.
Models.py
from django.db import models
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
You can start here Documentation
See also Django Girls Models

Django forms. How add, search data to form field from database

I have 2 models.
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Reservation(models.Model)
person = ForeiginKey(Perrson, on_delete=models.PROTECT)
from_date = models.DateField()
to_date = models.DateField()
I want make reservation.
How make form in order that first field "person" can search person and add found data to form field, if person not exist add new person.
I would suggest you have a look at django smart selects as it might come handy.
Your model would change as:
from smart_selects.db_fields import ChainedForeignKey
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Reservation(models.Model)
person = ChainedForeignKey(
Person,
chained_field="Person",
chained_model_field="Person",
show_all=False,
auto_choose=True,
sort=True)
from_date = models.DateField()
to_date = models.DateField()
Next you'd create a form and render it in a template. Whenever you select a person in the form reservation field will update automatically.
Then use a view to handle your logic, if person not exist add new person...

Categories