I am trying to design a database schema for a stock portfolio. What I have so far seems to work with one caveat --- a portfolio can contain two holdings of the same stock. I want a uniqueness of holdings with respect to the stock in the portfolio. I am new to Django, and haven't been able to figure it out.
class Stock(models.Model):
"""
All available stocks
"""
symbol = models.CharField(primary_key=True, max_length=4)
class Portfolio(models.Model):
"""
A user's portfolio containing stocks.
"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
usd = models.FloatField(default=0)
stocks = models.ManyToManyField(Stock, blank=True, through='Holding')
class Holding(models.Model):
"""
A holding of a stock in a portfolio.
"""
stock = models.ForeignKey(Stock)
amount = models.FloatField(default=0.0)
portfolio = models.ForeignKey(Portfolio, on_delete=models.CASCADE)
For example, in the admin interface I can create a Portfolio and then a Holding with amount=20 and stock='ABC'. I can duplicate that same holding, but I shouldn't be able to. The correct way would be to lookup the existing holding and add to amount.
Nevermind, got it...
Trick is to add unique_together in the intermediate table. It makes the stock unique in the portfolio.
class Holding(models.Model):
"""
A holding of a stock in a portfolio.
"""
stock = models.ForeignKey(Stock)
amount = models.FloatField(default=0.0)
portfolio = models.ForeignKey(Portfolio, on_delete=models.CASCADE)
class Meta:
unique_together = [('stock', 'portfolio'),]
Related
I have a database representing financial transactions. Columns representing payee and category are non-optional.
However, part of my app's functionality will be to ingest external spreadsheets of transactions which do not already have payee and category information. I would then populate a form where the user will select correct payees and categories through drop-down menus, and then save the completed information to the database.
Is the correct approach to simply create two separate but equivalent classes (see below)? Or is there some way to make one a sub-class to another, despite the fact that one is connected to a database and the other is not.
# An initial class representing a transaction read from an Excel sheet
# Payee and category information are missing at this stage, but will be filled
# in by the user later on
class TransactionFromSpreadsheet:
def __init__(self, date, amount):
self.date = date
self.amount = amount
self.payee = None
self.category = None
# The Django class that will be instantiated once all the user has completed all
# necessary information
class Transaction(models.Model):
date = models.DateField()
amount = models.DecimalField(max_digits=14, decimal_places=2)
category = models.ForeignKey('Category', on_delete=models.CASCADE)
payee = models.ForeignKey('Payee', on_delete=models.CASCADE)
One could use optional foreign keys and a custom manager to provide an easy way to query the "incomplete" or "complete" transactions.
class TransactionQueryset(models.query.QuerySet):
def complete(self):
return self.filter(category__isnull=False,
payee__isnull=False)
def incomplete(self):
return self.filter(category__isnull=True,
payee__isnull=True)
class TransactionManager(models.Manager):
def get_queryset(self):
return TransactionQueryset(self.model, using=self._db)
def complete(self):
return self.get_queryset().complete()
def incomplete(self):
return self.get_queryset().incomplete()
class Transaction(models.Model):
date = models.DateField()
amount = models.DecimalField(max_digits=14, decimal_places=2)
category = models.ForeignKey('Category', on_delete=models.CASCADE,
blank=True, null=True)
payee = models.ForeignKey('Payee', on_delete=models.CASCADE,
blank=True, null=True)
objects = TransactionManager()
And if you now need an incomplete transaction you could easily get these in a view:
def view_incomplete(request):
incompletes = Transaction.objects.incomplete()
return render(request, 'incomplete_template.html',
{'incompletes': incompletes})
It is now very comfortable to gather all heavily used filter conditions in the queryset and manager class.
And if you have non complementary filter conditions you could even chain the manager functions.
I have two tables Subjectlist and Day. Subject list is m2m in Day. So my problem is I'm creating school timetable. So for each days different subjects to be shown, when i add subjects on each days the order of subject is same.
#Models.py
class SubjectList(models.Model):
subject_name = models.CharField(max_length=25)
def __str__(self):
return self.subject_name
class Day(models.Model):
day_name = models.CharField(max_length=15)
subject_name = models.ManyToManyField(SubjectList)
class_number = models.ForeignKey(AddClass, on_delete=models.CASCADE, null=True, blank=True)
start_time = models.TimeField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)
def __str__(self):
return self.class_number.class_number
#Views.py
class TimeTableView(APIView):
def get(self, request, id):
class_number = AddClass.objects.get(id=id)
day = Day.objects.filter(class_number=class_number.id)
print(day)
serializer = DaySerializer(day, many=True)
return Response(serializer.data)
I want to do like this
Monday - English, maths, science, Social Science
Tuesady - Maths, Social Science, Englih, Math's
but i get like this
Monday - English, maths, science, Social Science
Tuesday- English, maths, science, Social Science
both are in same order even if add subjects in different order.
You can add more fields to the M2M table by declaring a model and assigning it to the M2M relation with the through parameter
class DaySubjectList(models.Model):
day = models.ForeignKey(Day, on_delete=models.CASCADE, related_name="day_subject_lists")
subject_list = models.ForeignKey(SubjectList, on_delete=models.CASCADE, related_name="day_subject_lists")
order = models.IntegerField(default=0)
class Day(models.Model):
day_name = models.CharField(max_length=15)
subject_name = models.ManyToManyField(SubjectList, through=DaySubjectList)
class_number = models.ForeignKey(AddClass, on_delete=models.CASCADE, null=True, blank=True)
start_time = models.TimeField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)
def __str__(self):
return self.class_number.class_number
You can set the order field using the through_defaults parameter of the related manager
my_day.subject_name.add(my_subject, through_defaults={"position": my_position})
You can order using related_name
Day.objects.filter(class_number=class_number.id).prefetch_related(Prefetch("day_subject_lists", queryset=DaySubjectList.objects.order_by("position")))
Records will only be ordered if you access my_day.day_subject_lists.all() not my_day.subject_name.all()
You can store those records under another name if thats more convenient
Day.objects.filter(class_number=class_number.id).prefetch_related(Prefetch("day_subject_lists", queryset=DaySubjectList.objects.order_by("position"), to_attr="my_prefered_name"))
be aware that my_day.my_prefered_name is a list not a queryet so don't use .all() to access it.
Use you have any issue with your models referencing each other you can use string synthax
class DaySubjectList(models.Model):
day = models.ForeignKey("myapp.Day", on_delete=models.CASCADE, related_name="day_subject_lists")
subject_list = models.ForeignKey("myapp.SubjectList", on_delete=models.CASCADE, related_name="day_subject_lists")
order = models.IntegerField(default=0)
You need to specify an order for the m2m relation [Django-doc]:
class Day(models.Model):
# ...
subject_name = models.ManyToManyField(
SubjectList,
ordered=True
)
# ...
Now if you query a Day object, the related SubjectList objects will be ordered in the order you added these.
You can further alter the order with .move(...), or .reorder(...).
Note: You should rename the subject_name field to subjects, since it relates to multiple SubjectList objects. A ForeignKey or OneToOneField indeed often uses the _name suffix, since these relate to at most one other object, but a ManyToManyField does not have this limitation.
Note: You should not use ForeignKey(.., null=True)s. A foreign key represents a relation, and a relation can not be "NULL". You should make these fields not nullable, and make the related model have a nullable parent relation (with null=True), or make the field optional with blank=True.
Image of django admin panel
This are my two models:
class English(models.Model):
date = models.DateField()
topic = models.CharField(max_length=150)
totalMarks = models.IntegerField()
def __str__(self):
return f"{self.id}. {self.topic}"
class Meta:
verbose_name_plural = "English"
class EnglishMarks(models.Model):
student = models.ForeignKey(UserField, on_delete=CASCADE)
test = models.ForeignKey(English, on_delete=CASCADE, related_name="marks")
marks = models.IntegerField(validators=[MaxValueValidator(English.objects.first().totalMarks),MinValueValidator(0)])
I want the marks field of EnglishMarks Model to not exceed the value of totalMarks field of related data of English Model.
Note: I am using Inlines to populate EnglishMarks table while creating new data in English table
I've got two models, an Invoice model and an Expense model. Upon creating invoices, the user should be able to add all expenses for an invoice. Once an invoice has been created - these expenses should somehow be "checked" so a new invoice doesn't add the same expenses. I'd need some form of check on the expense instance saying "has been invoiced" or something. Not entirely sure how to proceed with only a ..set.filter(...) queryset My two models looks like this:
Expense model
class Expense(Model):
cost = DecimalField(blank=True, null=True, max_digits=10, decimal_places=2)
project = ForeignKey(Project, on_delete=CASCADE)
user = ForeignKey(User, on_delete=CASCADE)
billable = BooleanField(default=False)
expense_date = DateField()
date_created = DateTimeField(auto_now_add=True)
invoice = ForeignKey("finances.Invoice", blank=True, null=True, on_delete=SET_NULL, help_text="Optionally link this to an existing invoice")
Invoice model
class Invoice(Model):
issue_date = DateField(default=now)
due_date = DateField()
project = ForeignKey(Project, on_delete=CASCADE)
amount = DecimalField(blank=True, null=True, max_digits=100, decimal_places=2)
include_expenses = BooleanField(default=False, help_text='Check this box to include all expenses that are within the invoice billable date range')
billable_start = DateField(help_text="Logged hours start date")
billable_end = DateField(help_text="Logged hours end date")
I tried adding a queryset on the Expense model as such:
def get_billable_expenses(self):
"""Getting all expenses for selected billable period"""
start_week = self.get_start_isoweek()
end_week = self.get_end_isoweek()
return self.project.expense_set.filter(
expense_date__gte=self.billable_start,
expense_date__lte=self.billable_end,
billable=True,
invoice=self.id
)
And then adding a signal that is triggered when an invoice is saved:
#receiver(post_save, sender=Invoice)
def prepare_calculation_data(sender, instance=None, created=False, **kwargs):
if created:
expenses_to_add = []
for expense in instance.get_billable_expenses():
expense.invoice = instance
expenses_to_add.append(expense)
Expense.objects.bulk_update(expenses_to_add, ['invoice'])
Not sure how to handle this - I thought I'd solve it by checking if the expense already has an invoice tied to it - if not - include it in the newly created invoice (as seen in get_billable_expenses() above) but that means no new expenses will ever be added either as the invoicefield won't be empty
Any suggestions on how to solve it?
I solved it by having two methods on my Invoice model. One to query for un-invoiced expenses:
def get_billable_expenses(self):
"""Getting all expenses for selected billable period"""
start_week = self.get_start_isoweek()
end_week = self.get_end_isoweek()
return self.project.expense_set.filter(
expense_date__gte=self.billable_start,
expense_date__lte=self.billable_end,
status='approved',
billable=True,
invoice__isnull=True
)
and then in my template showing those expenses as such:
def get_linked_expenses(self):
return self.project.expense_set.filter(
invoice=self.id
)
I am currently learning Django, and I am finding it a bit difficult wrapping my head around the ManyToMany fields. I am using an intermediate model to manage my relationships.
I have three models; Ticket, User, and TicketUserRelation.
I want to be able to query the ticket model, and retrieve both its corresponding user objects and the ticket object. How would I go about doing this?
In Laravel I would do something along the lines of
Ticket::where('id', '1')->with('contributors')
But I can't really figure out how to do this in Django
The models:
class User(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Ticket(models.Model):
contributors = models.ManyToManyField(User, through=TicketUserRelation, related_name='tickets')
name = models.CharField(max_length=50)
created_at = models.DateField()
def __str__(self):
return self.name
class TicketUserRelation(models.Model):
id = models.AutoField(primary_key=True, db_column='relation_id')
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
EDIT: I am using an intermediate model so that I can easily add things like join date later.
You don't need the TicketUserRelation model when using Django ORM. You could simply use a ForeignKey in the Ticket model, or use the ManyToManyField already defined, if one ticket can be assigned to multiple users.
class Ticket(models.Model):
# For one user, use ForeignKey
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tickets')
# For multiple users, use ManyToManyField
contributors = models.ManyToManyField(User, related_name='tickets')
name = models.CharField(max_length=50)
created_at = models.DateField()
def __str__(self):
return self.name
You can then get all tickets for a user u with:
u.tickets.all()
Figured it out myself, using prefetch_related. I was having trouble understanding how prefetch_related works. For those that are confused too, from my understanding it works like this:
Ticket.objects.all().prefetch_related('contributors')
This returns a queryset, something along the lines of this
<QuerySet [<Ticket: Testing ticket one>, <Ticket: Testing ticket two>, <Ticket: Testing ticket three'>, <Ticket: Testing ticket four>]>
When you then access the elements in the queryset, you can then call .contributors on the object, like so:
# Get the queryset
tickets_with_contribs = Ticket.objects.all().prefetch_related('contributors')
# Print the contributors of the first ticket returned
print(tickets_with_contribs[0].contributors)
# Print the contributors of each ticket
for ticket in tickets_with_contribs:
print(ticket.contributors)
Looking back at it this should have been pretty self explanatory, but oh well.