I am trying to make a small inventory app in a Django app. I want to be able to see the stock on hand, where it is (between two places and all together), purchase to add to inventory, and sell to reduce inventory. Tracking sales would be nice. So all in all, there are about five things I would like to do. To track the location, I was thinking of listing warehouse one as 1 and two as 2, and could query WHERE field = 1, but if I have the same product in two places, I would run into an issue. I also don't know the best way to approach it. Reading through post here, I see some people saying to just track sales, and total them in a flat db, others to remove from the db, but that would lead to discrepancies. This is my model.py so far, just really stabbing at the dark here, looking for some guidance.
from django.db import models
class Product(models.Model):
product_ID = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
type = (
('WHT' , 'White Wine'),
('RED' , 'Red Wine'),
('SPRT', 'Spirits'),
('BR' , 'Beer'),
)
size = (
('12' , '12oz'),
('16' , '16oz'),
('375' , '375mL'),
('750' , '750mL'),
('1' , '1L'),
)
amount_per_case = models.IntegerField()
warehouse = (
(1 , 'One'),
(2 , 'Two'),
)
on_hand = models.IntegerField()
class Store(models.Model):
store_ID = models.IntegerField(primary_key=True)
store_name = models.CharField()
store_type = (
('Supermarket', 'Supermarket'),
('Liquor Store', 'Liquor Store'),
)
store_address = models.CharField()
store_phone = models.CharField()
store_contact = models.CharField()
class Vendor(models.Model):
vendor_ID = models.IntegerField(primary_key=True)
vendor_name = models.CharField()
vendor_address = models.CharField()
vendor_phone = models.CharField()
vendor_contact = models.CharField()
class Sale(models.Model):
sale_date = models.DateField()
sale_product = models.ForeignKey(Product)
You should make a Warehouse model and create a many-to-many relationship between Product and Warehouse, using the through option to specify another model to hold relationship details called something like WarehouseProduct, in which you store the counts. See this section of the docs, and the following section.
P.S. You can do much the same thing to associate sales with particular stores, which seems to me to be something that would probably be of interest to you, as well.
Related
There are many questions about building model forms from multiple tables, but I couldn't find a solution to my problem.
Background:
In model.py I have 3 simple models with 1-many relationship
class site(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
sitename = models.CharField(max_length=150)
site_address = models.CharField(verbose_name='Address', max_length=200, null=True, blank=True)
class block(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
blockname = models.CharField(max_length=150)
sitename = models.ForeignKey(site, on_delete=models.CASCADE, )
class quarter(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
quartername = models.CharField(max_length=150)
blockname = models.ForeignKey(block, on_delete=models.CASCADE, )
I have separate forms for each model - where user can add, edit and delete and site, block using standard djando modelform and modelformset.
The challenge I'm facing is with creating a quarter form. On a quarter from I want user to select a site from drop-down, which will populate block and then use can enter quarter name and save submit.
Now, 1 possible solution is to include site as FK in quarter and that will solve the problem - but I don't want to do that. I even tried create separate form instances and call it in view and template - but the problem with this solution is that the mandatory field name from site are also displayed.
How can I show the sitename dropdown on quarter form - where on selecting sitename the user is shown corresponding blockname. On click of save button the data for quarter is saved in db (don't have to save site or block data because we already have those entries)
Any suggestion on approach or solution?
I am creating a tool where users can follow each other. The way I have Implemented this is by extending the user model in Django as people model.
here is the people Model ->
class People(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,related_name='people')
Name = models.CharField(max_length=255,null=True)
following = models.ManyToManyField(to=User, related_name='following', blank=True)
photo = models.ImageField(upload_to='profile_pics', blank=True,null=True)
Phone_number = models.CharField(max_length=255,null=True,blank=True)
Birth_Date = models.DateField(null=True,blank=True)
Created_date = models.DateTimeField(auto_now_add=True)
Updated_date = models.DateTimeField(auto_now=True)
Here the poeple model is extended from user model and the relation is defined using oneToOneField.
Now the following column has many to many relation to the user model.
The Schema of the people_following table is that is has 3 columns
ID -> unique Identified (Primary Key)
People_id = user that has been followed.
User_id = user who is following(Logged in user)
suppose user with ID = 2 is logged in and wants to follow the User with ID = 7
then following will be the row in the table ->
ID people_id user_id
1 7 2
Now I would like to count the no of followers and the no of people following a particular user(Logged In user)
here is the Query ->
user_id = pk
person = People.objects.select_related('user').get(user_id=user_id)
context = {
'followers': person.following.filter(people_id=user_id).count(),
'following': person.following.filter(user_id = user_id).count(),
}
But this is giving me error ->
cannot resolve keyword -> people_id.
can anyone help me out with this.
I am trying to get down through multiple-foreign key relationship where each Hotel has many Rooms, each Room has many Rateplans, each Rateplan has many Prices.
It resembles Christmas tree if you think about it:
Hotel
v
Room
v
Rateplan
v
Prices
How can I execute query that will return hotels in certain hotel_city on certain price_date, certain price_price and certain price_availability? (just like Expedia or booking.com query)
For example:
Hotel.objects.filter(city='Beirut').filter(room__name=Room.objects.filter(rateplan__name=Rateplan.objects.filter(prices__availability=Prices.objects.filter(availability=1))))
I have looked into django complex queries documentation and annotate/aggregate but couldn't wrap my head around it.
My models below:
class Hotel(models.Model):
name = models.CharField(max_length=64)
city = models.CharField(max_length=64, default='Warsaw')
class Room(models.Model):
hotel_id = models.ForeignKey(Hotel, on_delete=models.CASCADE, related_name='room')
name = models.CharField(max_length=64, default='Double')
class Rateplan(models.Model):
room_id = models.ForeignKey(Room, on_delete=models.CASCADE, related_name='rateplan')
name = models.CharField(max_length=64, default='Standard')
class Prices(models.Model):
rateplan_id = models.ForeignKey(Rateplan, on_delete=models.CASCADE, related_name='prices')
date = models.DateField()
price_1 = models.DecimalField(null=False, max_digits=7, decimal_places=2)
price_2 = models.DecimalField(null=False, max_digits=7, decimal_places=2)
availability = models.SmallIntegerField()```
You can use __ to filter the values you need across relationships
There are good examples in the documentation
Hotel.objects.filter(
city=your_city,
room__rateplan__prices__date=your_date,
room__rateplan__prices__price_1=your_price,
room__rateplan__prices__availability=your_availability,
).distinct()
This is my models:
class Persons(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
members = models.ManyToManyField(
Persons,
related_name='group_members'
)
I am trying to get all the group members like this:
p = Persons.objects.select_related('group_members').values('id')
and it returns all the Person id, this is the problem.
I want only person id who are in any of the group members..
Can anyone help me in this case?
i suggest to make a title for the group like this
class Group(models.Model):
name = models.CharField(max_length=20)
and then make a group for example name it 'GOT fans', then assign it to multiple users.
then select all the 'Persons' in that particular group like this
gotFansIDS = Group.objects.filter(name="GOT fans").values('id')
Using the standard Django example model for M2M relationships. I would like to return a list of Person (people) that don't have a Membership (ie. no related records in the Membership table. How would I go about doing this? Is anybody able to point me in the right direction?
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Thank you.
Person.objects.filter(membership__isnull=True)
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#isnull