Django: How to create a field with many booleanfields? - django

I am trying to create a days_of_week model for my alarm app where users can just select the days of the week they want the alarm to go off. I want to use checkboxInput widget for it, but I can't find a way to find a field like that. Can someone show me how to make a field like that?

You can use CommaSeparatedIntegerField. More info in https://docs.djangoproject.com/en/dev/ref/models/fields/

You need to use a checkboxselectmultiple:
https://docs.djangoproject.com/en/dev/ref/forms/widgets/#checkboxselectmultiple
short example:
select = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple)
where CHOICES would be your days of the week.

Related

Django Query without referencing fields

Quick overview:
I have a Forecast model setup which has a workflow_state.
Now I'm trying to query the Forecast for all the forecasts that are in a certain state AND the current person logged in is_staff.
If i was writing a raw query this wouldn't be an issue because i could write something like:
SELECT * FROM forecast WHERE forecast.workflow_state_id in (1,2,3,4) AND 1 = user.is_staff
However, when trying to write this in a queryset I can't figure out how to reference a constant. I don't want to write a raw queryset and if possible want to avoid using the extra field.
Any suggestions would be appreciated. Thanks
Your constant is only a True
Forecast.objects.filter(workflow_state__in=[1,2,3,4], user__is_staf=True)
Your edit makes things rather less than clear, but you seem to be asking how to do a check on the current logged-in user, rather than on the user referenced by the model. In which case, you don't do that in a query at all; your example SQL statement wouldn't work, and neither would doing it in the ORM. You do it in Python, of course:
if request.user.is_staff:
forecasts = Forecast.objects.filter(workflow_state__in=[1,2,3,4])

Django store the original value of a field

I am a newbie with Django trying to create a dashboard application reporting on some key milestone dates. I want to be able to track how the key dates are changing. For example:If the kick off date has been changed 5 times I want to be able to report 1. the first date entered, 2. Current date, 3. The date before the last update.
Thank you
Your question is not clear. But for the logic you have asked one thing we can do is to make a model in which the edited dates and user will be fields. Use user as foreign key of your User model. I will just give an example model.
class Dates(models.Model):
event = models.ForeignKey(Event)
date = models.DateField()
This is a very basic method which i am saying. This is a bit complex and you will have to check if the field has changed five times and all.
For a better answer please make the question clear.

Creating select modelform based off min and max year present in table?

I am attempting to create a ModelChoiceField who's options are based off finding the years present in a number of posts within the database.
So for example there are posts stored in my database with a DateTime field. What i am attempting to do is create a form with the options based on the range of years present in the post table.
ie
We have a post with field date_created. The value is
2013-06-27 03:21:15
and we have another with the value
2012-06-27 03:00:20
I want to display a form with select options for each year present in the post table with no duplicates;
<option value="2013">2013</option>
<option value="2014">2014</option>
Will a ModelChoiceField allow me to do this with a single query or do i need to process and build say a list containing the years.
Thanks to lalo for pointing me in the right direction. I managed to figure it out using the following;
years_list = []
choices = []
year_list = Post.objects.all().values_list('date_created').distinct()
for years_avail in year_list:
years_list.append(years_avail[0].strftime('%Y'))
for year in years_list:
choice =(year, year)
choices.append(choice)
years = forms.ChoiceField(choices, label='')
Is this a good way to go about doing this or is this inefficient/wrong?
ModelChoiceField is for model select, based in foreign key. I think you have to user ChoiceField and generate the options in runtime.
Show me where you use the form, maybe can show you how to do.

Retrieve a select field value to fill a form field

I would like to use a ready-made select list of all the world countries (I don't want to store it in database) to fill an input field of a Django modelform, but I don't know which way to go.
Is there a way to do so?
http://code.google.com/p/django-countries/
Enjoy!
The only way I know to do that is to hardcode it into the template.
This list has it done already:
http://snippets.dzone.com/posts/show/376
by following this guide you can have automatic countries populated in your form field
http://code.google.com/p/django-countries/source/browse/trunk/docs/overview.txt

Get daily counts of objects from Django

I have a Django model with a created timestamp and I'd like to get the counts of objects created on each day. I was hoping to use the aggregation functionality in Django but I can't figure out how to solve my problem with it. Assuming that doesn't work I can always fall back to just getting all of the dates with values_list but I'd prefer to give the work to Django or the DB. How would you do it?
Alex pointed to the right answer in the comment:
Count number of records by date in Django
Credit goes to ara818
Guidoism.objects.extra({'created':"date(created)"}).values('created').annotate(created_count=Count('id'))
from django.db.models import Count
Guidoism.objects \
# get specific dates (not hours for example) and store in "created"
.extra({'created':"date(created)"})
# get a values list of only "created" defined earlier
.values('created')
# annotate each day by Count of Guidoism objects
.annotate(created_count=Count('id'))
I learn new tricks every day reading stack.. awesome!
Use the count method:
YourModel.objects.filter(published_on=datetime.date(2011, 4, 1)).count()