I am fairly new to Django and am hoping to get some help on a problem that I’m trying to solve. I’m building an inventory application. I have a dish model that contains the fields: name, price, category, and active (a Boolean to specify whether it is in rotation or retired.) I want to be able to set the quantity of each active dish in a form and have each quantity be displayed in a log along with date, time, user, etc. I also want to be able to take those quantities, add them to the current inventory of active dishes, and display the updated inventory of each dish. Ideally I would make a quantity field for each instance of active dish in a separate class (that would automatically spawn and cease when dish objects are created, deleted, or switched from active to inactive and vis versa by admin) then make a model form from this, but Django does not seem to have this functionality. I have found possible solutions to my problem using modelformset or a JSON Schema but I’m interested to know recommendations for my specific problem.
Welcome @Meredith !
So if I can try to make this more specific, you’re talking about a model that may look something like this:
class Dish(models.Model):
name = models.CharField(...)
price = models.DecimalField(...)
category = models.CharField(...) # or models.ForeignKey(Category)
active = models.BooleanField(...)
quantity = models.IntegerField(...)
Beyond this, I’m not sure I’m clear on what it is you’re trying to do here.
Why would this quantity be in a separate class?
What is intended to be “automatically spawned”?
Can you be more specific, or provide more details about what exactly it is that you’re trying to do?
I must admit that my initial reaction to what I’m reading here is that you would be better served by re-thinking your model structure, but such a conclusion is premature without a better understanding of the underlying requirements.
Hello,
Thank you for your response! Re-thinking my model structure may absolutely be the answer:
I want to be able to take data on how many of each active dish was made that day in a single form rather than one form for each dish (see picture 1.) After that, I want the values of the submitted form to show up in a log along with date, time, user, etc (see picture 2.) Then I want to take those quantities and add them to the current inventory of active dishes then display those added quantities (see picture 3.) Quantities in all pictures are arbitrary.
Cool! Ok, this raises a number of questions. (I’m trying to make sure I understand everything that is going on.)
Page 1 - titled “Kitchen Form”.
You want to generate one line on a form for each Dish
where active == True
. Correct?
Each line shows the name
as a non-editable label, and an integer input field for quantity
. Correct?
These numbers are going to be saved back in the database with their corresponding name
. Correct?
If my assumptions are correct, then what you are looking at generating is a Django ModelFormset, where each form renders the name as the label, and allows entry of only the quantity
field.
Page 2 - Kitchen log.
You identify this as a “log”:
Where do these date
, time
, and user
information come from?
Is each person going to fill out the “Kitchen Form” such that when I enter that form, it’s going to say something like:
Date | User | Dish 1 | Dish 2 | Dish 3 |
---|---|---|---|---|
8/26/2024 | Ken | 5 | 8 | 7 |
If you fill out this form, then the table may end up looking like this:
Date | User | Dish 1 | Dish 2 | Dish 3 |
---|---|---|---|---|
8/26/2024 | Ken | 5 | 8 | 7 |
8/26/2024 | Meredith | 12 | 9 | 15 |
And then the resulting Inventory page would look like this:
Dish | Quantity |
---|---|
Dish 1 | 17 |
Dish 2 | 17 |
Dish 3 | 22 |
Am I correct so far?
If so, then the original model is not adequate. You are likely going to want to remove quantity
from that model, and create another model that for discussion purposes I will call DishPrep
. It may look something like this (simplified, not syntactically correct):
class DishPrep(...):
prep_date = DateTimeField(...)
cook = ForeignKey(User, ...)
dish = ForeignKey(Dish, ...)
quantity = IntegerField(...)
Your inventory for each dish would then consist of the sum of the quantity fields for each dish for a given date. (Am I understanding that correctly?)
Side note: The forum does provide an easy way to create a table in a post. The second table above was created with this text:
| Date | User | Dish 1 | Dish 2 | Dish 3 |
|---:|---|:---:|:---:|:---:|
| 8/26/2024 | Ken | 5 | 8 | 7 |
| 8/26/2024 | Meredith | 12 | 9 | 15 |
Notice how I did not need to worry about aligning anything. Creating that header line and the second line with the three dashes was enough to make the forum software create that table.
Thank you so much!
Everything you said is correct. The only differences that I have in mind is that the Kitchen log would display the newest entry on top and entries would be descending by datetime rather than ascending. Datetime will come from the date and time of the entries and user will come from the user who made the entry. And inventory would display addition of all kitchen entries, not just one day so perhaps I can have a field that stores that value somehow then add and subtract from it rather than adding and subtracting a huge quantity of numbers every time it is viewed.
Thank you for the side note about the available forum table syntax!
Cool, I can see that.
Is there anything that would cause this number to be reduced, or is it always increasing?
Can someone go back into the system later the same day to update that day’s entry? Can a previous day’s entry be updated?
That idea needs to be considered with a lot of caution. (Adding the totals up dynamically is not likely to create an issue in the common case. However, there are ways to mitigate / reduce this depending upon how all these values can be modified.
My apologies for not responding for so long. I got sick but I am still working on the same project. I’m going to have three other pages: distribution, market, and sold. Distribution will have a form (similar to kitchen) that allows a user to enter how many of each dish went to which markets (user specifies which market then number of each dish.) Once a distribution form is submitted, those dish quantities get subtracted from the current inventory (which I imagine will be a list of the dishes with a quantity to the right of each dish,) an inventory log will record the inventory with a time stamp (which will contain one row per change in inventory,) and a distribution log will record the quantities from that form submission with a timestamp (one row per form submission.) When a user comes back from the market, there will be a Market form in which, the user will input all of the quantities of dishes that remain (dishes that weren’t sold at the markets.) The market form submission will update another page called sold which will be a log with each row representing the number of dishes sold at a given market, on a given day (I will get this number by subtracting the market form quantities from the distribution form quantities with matching market and day.
And yes, someone can go back into the system later the same day to update that day’s entry and a previous day’s entry can be updated. I think previous day entries should only be updateable by admin though.