I'm working on a website that has to do with user entering data to fields of a table. There is (input type=date) in top of the page. I want the user to enter values daily. but I want to save that data in the database according to that day and if the user change the date, I want to clear some fields for that day for a new fresh entry.
I also don't know if there should be a datefield in model or not?? If so, would I have to populate the database with lots of dates? and if it reach the last date, I have to populate it again and so on??
How I go about doing this?
I tried looking for a solution but couldn't find one that seems good. I found I could use crontab in django or scheduler but I don't think this is the optimum solution.
Related
The form that I need to do is for the user to select a payment type.The problem is that depending on the choice if the user wants to pay monthly or annually a different date-picker field has to appear. if he selects monthly, he has to choose a day of the month. And if he chooses annually a date-picker that allows him to pick the month has to appear.
I've been searching online for an answer but all I can find is dynamic form in the sense that it can generate a field type multiples times and that is not what I need.
All help is appreciated.
The way I'd approach this is to have optional fields for both the month and year pickers, but use Javascript to hide the one you don't want the user to see based on the value in the payment type. Then, in the clean method of the form, I'd validate that correct field was filled in (maybe raise a ValidationError if the both were filled?).
Alternatively, depending on what you're actually storing as a result from the datepicker (just the year or month?) maybe it'd be simpler to just dynamically update the datepicker's configuration based on the payment type field's value.
It's a little tricky to give you more specific suggestions without having more information about how you've setup your code.
In my form I have a field that I want to default as Today when launching the form:
class ExpenseForm(forms.ModelForm):
date = forms.DateField(initial=datetime.now(), widget=forms.DateInput(attrs={'id':'datepicker'}))
Now if I go to the form I see it working correctly, it's got today's date populated, (for example "2018-12-03") but then when I come in tomorrow, it STILL has yesterday's date populated. In fact it will not update until I somehow restart the server.
Any idea?
Thank you.
Scenario:
Developing a question answer app.
Here are different users can answer the questions.
Each question may have several fields to response (2 or 3 yes/No checkboxes) and any user can update any of those any time.
Problem:
I need to keep a log (with time and user name) in a different log table every time the records got any changes.
The log table is just a look alike of the original model (e.g. ChangeLogModel) just with 2 extra fields as logDate and ChangingUser.
This will help me to check the log and find the status of the question in any specific date.
Possible Solutions:
Using signals (...Not used to with signals, lack of detailed tutorials, documentation is not detailed too)
making the backup before doing any ".save()" (... Have o idea how to do that)
Install any external app (...Trying to avoid installing any app)
Summary:
Basically What I am asking for is a log table where the 'state' of the original record/row/tuple would be saved to another table (i.e. logTable) prior to hit the "form.save()" trigger.
So, every time the record got updated so the LogTable will get a new row with a datestamp.
You could use an django package for audit and history, any of those in this overview for example.
I had success using django-simple-history.
I think that the best way is just to do it straight forward. You can save the user's answer and right after that the log, wrap it with database transaction and rollback if something goes wrong.
Btw if the logs table has the same fields like the original model you might consider using foreign key or inheritance, depends on your program logic.
I am trying to write a python statement to calculate a user's dob in a mysql db. The dob is a auto-populated field (based off his profile) in a form that a user fills out.
We write python code directly into the fields in our mysql db that perform calculations on runtime. An example:
date.today().strftime("%m-%d-%Y") for today's date.
But I have never used it in reference to another field on the same form, so I am unsure how to do this. I have tried:
(date.today() - dob) / timedelta(days=365.2425).strftime("%m-%d-%Y")
where dob is the field on the form where the user's date of birth is auto-populated.
What am I missing? I receive a 'syntax error', probably because it doesn't know what the dob variable is, but I don't know how to point to that field's value to make it perform the calc. Any help would be appreciated!
So, just in case you find yourself using Python and MySQL, here is the correct answer - this must be placed in a field designated to pull from the db, and depending on the code, it would be:
round(datediff(now(), s.dob))/365.25
or
concat(round(datediff(now(), s.dob))/365.25)
where s.dob is a field in another table. Not sure if this will help anybody, but thought I would post in nevertheless. Thanks!
I am setting up a simple billing system, where I have a table that lists users and the day they are supposed to be billed. Each user only has one row of this table associated with them.
I need to query the database on a daily basis to get a list of users to be billed on that day.
The model is:
class BillingDay(models.Model):
user = models.ForeignKey(User)
day = models.IntegerField(max_length=2)
How would I query against the day field? User.objects.filter(billingday=1) looks at the ID, but I'm looking i need to get a list of users with 1 as the value for day in billingday
User.objects.filter(billingday__day=1)
Just as a note, though, you might want to rethink how you're setting this up before you get too far down the rabbit hole. Will users have multiple billing days? My guess would be no. If that's the case, there's no reason for a BillingDay model. It only adds complexity and fragments data. The billing day could just be a field on your user profile.
Now, creating a user profile for a User is in principle no different that having a BillingDay model as a way to add extra data to User, but it's far more extensible. Django has builtin methods for having a user profile associate with every User, and you can more data to the same user profile object over time. Whereas, BillingDay would be relegated to just one data point and you'd later have to add additional models (more complexity and fragmentation of data) for other data points down the line.
See Django's documentation on user profiles.