I have this "Jobs Server" model that i'm building. I want to include a field that will save which days of the week this job will run on. Ultimately in the UI, i would like the user to be able to have a series of check boxes(one for each day) that they can select. What would be the best way to represent this "days-of-week" data in my mode?
class Job(models.Model):
name = models.CharField(max_length=32, unique=True)
package = models.ForeignKey(Package)
binary = models.ForeignKey(Binary)
host = models.ForeignKey(Host)
colo = models.ForeignKey(Location)
group = models.ForeignKey(Group)
type = models.ForeignKey(Type)
start = models.TimeField()
end = models.TimeField()
days = ?
You may want to create DayOfTheWeek field type, which you can improve in various ways.
This code cause to translate automatically into the local language using the multilingual tools.
#myFields.py
from django.utils.translation import ugettext as _
DAY_OF_THE_WEEK = {
'1' : _(u'Monday'),
'2' : _(u'Tuesday'),
'3' : _(u'Wednesday'),
'4' : _(u'Thursday'),
'5' : _(u'Friday'),
'6' : _(u'Saturday'),
'7' : _(u'Sunday'),
}
class DayOfTheWeekField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs['choices']=tuple(sorted(DAY_OF_THE_WEEK.items()))
kwargs['max_length']=1
super(DayOfTheWeekField,self).__init__(*args, **kwargs)
#models.py
import myFields
(..)
dayOfTheWeek = myFields.DayOfTheWeekField()
(..)
Something like this would work.
#models.py
DAYS_OF_WEEK = (
(0, 'Monday'),
(1, 'Tuesday'),
(2, 'Wednesday'),
(3, 'Thursday'),
(4, 'Friday'),
(5, 'Saturday'),
(6, 'Sunday'),
)
days = models.CharField(max_length=1, choices=DAYS_OF_WEEK
#forms.py
widgets = { 'days': forms.CheckboxSelectMultiple }
Or to save multiple days
#models.py
class Days(models.Model):
day = models.CharField(max_length=8)
days = models.ManyToManyField(Days)
#forms.py
widgets = { 'days': forms.CheckboxSelectMultiple }
If you want a checkbox for each one, then the easiest thing to do is to create BooleanFields for each of them. If you want to store it as a more complex value (eg. comma separated list or something), create your own widget and play with javascript, then you could go that route.
Just implemented django-weekday-field. Works great!
Hopefully this helps other people who stumble upon this question
EDIT: updated link to pypi since the bitbucket repo was deleted.
It hasn't been updated since 2014 but looking at the code is a great way to get started on answering this question
Related
My goal is to be able to select a location and Input part numbers without seeing this quote field. I dont even completely understand what this select box is looking for. I have Quote objects saved and yet these are not coming up as selectable options. Not that I want them to, Im just saying. My thinking regarding the seelctable options is that this would be auto-populated? You can probably tell my confusion even in my explanation. Ultimately, I dont want to see a select box at all as Im not really interested in whatever this pointing to, but just for kicks would like to know what it is trying to point to.
quote/Models.py
class Quote(models.Model):
QUOTE_ENVIRONMENTS = (
('testing', 'Test'),
('production', 'Production')
)
SALES_SOURCE=((1, 'Marketplace'),
(2, 'Webstore'),
(3, 'Physical Store'),
(4, 'Phone')
)
environment = models.CharField(max_length=20, choices=QUOTE_ENVIRONMENTS, default="testing")
sales_source = models.IntegerField(choices=SALES_SOURCE, null=True)
order_notes = models.TextField(blank=True)
locations = models.ManyToManyField('products.ProductSelection')
products/models.py
class Product(models.Model):
pass
class Warehouse(models.Model):
pass
class ProductSelection(models.Model):
location = models.ForeignKey('Warehouse', on_delete = models.CASCADE)
product = models.ManyToManyField('Product')
Admin.py
class ProductOrderForm(forms.ModelForm):
locations = forms.ModelChoiceField(queryset= Warehouse.objects.all())
part_number = forms.IntegerField()
def clean_product_id(self):
cd = self.cleaned_data
logger.info(cd)
value = cd['part_number']
if value not in Products.objects.list_part_numbers():
raise forms.ValidationError("Not a valid partnumber")
class ProductSelectionTabularInline(admin.TabularInline):
form = ProductOrderForm
model = Quote.locations.through
class QuoteAdmin(admin.ModelAdmin):
list_display=['id', 'environment', 'order_notes','sales_source']
list_editable = ['environment', 'sales_source', 'order_notes']
inlines = [ProductSelectionTabularInline]
exclude=['quote']
Error when using exclude attr.
ERRORS:
<class 'orders.admin.ProductSelectionTabularInline'>: (admin.E201) Cannot exclude the field 'quote', because it is the foreign key to the parent model 'orders.Quote'.
I dont want the left most box. Thanks for your help
I figure out that the field to the left is the ProductSelection instance. I confused myself by adding the other 2 form widgets. So this does not allow me to do what I want which is to edit the parts to the locations and add it to the form for creating a quote.
I'm learning Django and looking for a best practice:
Imagine I have a model for a mobile phone device:
class Device(models.Model):
vendor = models.CharField(max_length=100)
line = models.CharField(max_length=100, blank=True)
model = models.CharField(max_length=100)
Let's say I create an object like this:
Device.objects.create(vendor = "Apple",
line = "iPhone",
model = "SE"
)
or without "line":
Device.objects.create(vendor = "Xiaomi",
model = "Mi 6"
)
Then I'd like to track sales in my shop for every device, so I create a model for a "Deal" (I track only the deal date and the device sold, device as a ForeignKey):
class Deal(models.Model):
device = models.ForeignKey(Device, on_delete=models.CASCADE)
deal_date = models.DateTimeField(default=None)
Question:
What is the best way to create a "Deal" object, if I want to query "Device" by its full, concatenated name, e.g. "Apple iPhone SE" or "Xiaomi Mi 6"?
I've found something similar in Django database entry created by concatenation of two fields , however not sure if it's the right path in my case.
My best guess is something like this (where "name" is a concatenated field):
de = Device.objects.get(name = "Apple iPhone SE")
Deal.objects.create(device = de,
deal_date = datetime(2018, 4, 26, 15, 28)
)
What is the correct way to do this task? Many thanks for your help!
Thanks for your advice guys, searching a little bit more I've found an answer appropriate in my case:
what I did is I tweaked save() method, which now populates a field automatically as a concatenation of 3 other fields.
#propertywas usefull in this case too
Supposing that you var name contains your text search criteria, and usign your data models, you could use annotation to create a field to each object returned by your query set, and then filter using this field
You could try some as follow (it is not tested)
import datetime
from django.db.models import F
from your.app.models import Deal, Device
# supposing is in your view, but like sounds as a model method
def my_view(request, *args, **kwargs)
name = request.POST.get('name')
device_qs = Decive.objects.all().annotate(text_concatenated='{} {} {}'.format(F('vendor'), F('line'), F('model'))).filter(text_concatenated=name)
try:
device = device_qs.get()
except Device.DoesNotExist:
# to manage the scenario when doesn't exist any match
# here you manage this situation
pass
except Device.MultipleObjectsReturned:
# to manage the scenario when various devices have the same 'text concatenated', i dont know, maybe data model should be improved
# here you manage this situation
device = device_qs.first()
deal = Deal.objects.create(device=device, deal_date=datetime.datetime.now())
# build your response and return it
I have three models: Business, Offers and OfferPlan:
Business:
class Business(models.Model):
name_of_business = models.CharField(max_length=255)
Offers:
class Offers(models.Model):
business = models.ForeignKey(Business, related_name="business_offer",
on_delete=models.CASCADE)
title = models.CharField(max_length=255)
subtext = models.CharField(max_length=255)
OfferPlan:
class OfferPlan(models.Model):
WEEKDAYS = [
(1, _("Monday")),
(2, _("Tuesday")),
(3, _("Wednesday")),
(4, _("Thursday")),
(5, _("Friday")),
(6, _("Saturday")),
(7, _("Sunday")),
]
offer = models.ForeignKey(Offers, related_name="business_offer_plan",
on_delete=models.CASCADE)
weekday = models.IntegerField(
choices=WEEKDAYS,
)
from_hour = models.TimeField()
to_hour = models.TimeField()
I have a ListView which search for businesses open based on different params such as city, category etc. I also want to now search by weekday, say which business is open on Monday will be displayed and which are not wont be displayed on that day. Weekday information is stored in OfferPlan and there could be multiple timings for the offers that day in OfferPlan table, but I want to query (filter, exclude) the businesses who has even a single entry on that weekday number.
Here is my ListView:
class SearchListView(ListView):
template_name = 'search/search.html'
model = Business
def get_queryset(self):
# queryset = Business.objects.filter(business_address__city=AppLocations.objects.first().city)
if 'city' in self.request.GET:
queryset = Business.objects.filter(business_address__city=self.request.GET.get('city'))
if 'category' in self.request.GET:
queryset = queryset.filter(category__code=self.request.GET.get('category'))
# if 'date' not in self.request.GET:
# queryset = B
raise
return queryset
How could this be possible? Also looked into https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/ but not able to figure out.
Thanks
Update 1
After researching more in the web, I figured out this is how it could be achieved, but need to know for sure from other Django enthusiasts here that it is right.
queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gt=1)
Solution
Jefferson's solution was tagged as right answer as it provided more insights, about which query is fast and what wrong was with my previous update, so here is the proper solution to which we both agreed:
queryset.filter(business_offer__business_offer_plan__weekday=1).annotate(count_entry=Count('business_offer__business_offer_plan__weekday')).filter(count_entry__gte=1)
def get_query(weekday):
businesses = Business.objects.filter(business_offer__in=Offers.objects.filter(
business_offer_plan__in=OfferPlan.objects.filter(weekday=weekday))).distinct()
return businesses
There's a heavy query, but it works.
There's no conditional expression here - and your annotation is much too complicated. You just need an additional filter.
queryset.filter(business_offer__business_offer_plan__weekday=self.request.GET['weekday'])
all:
What is the correct Model fieldtype to use with a CheckboxSelectMultiple widget for static data? I am receiving validation errors and feel I'm missing something simple.
The app is a simple Django 1.6 app in which a Campground object can have multiple eligible_days (e.g. Site #123 may be available on Monday & Tuesday, while Site #456 is available on Weds-Friday).
Because it's static data and I've ready that a ManyToManyField has unnecessary DB overhead, I'm trying to do this with choices defined inside the model, but when I try to save I get the validation error Select a valid choice. [u'5', u'6'] is not one of the available choices. every time.
Q1: Do I have to override/subclass a field to support this?
Q2: Do I need a custom validation method to support this?
Q3: Am I making things unnecessarily hard on myself by avoiding ManyToManyField?
Thank you for your help!
/m
models.py
class CampgroundQuery(models.Model):
SUN = 0
MON = 1
TUE = 2
WED = 3
THU = 4
FRI = 5
SAT = 6
DAYS_OF_WEEK_CHOICES = (
(SUN, 'Sunday'),
(MON, 'Monday'),
(TUE, 'Tuesday'),
(WED, 'Wednesday'),
(THU, 'Thursday'),
(FRI, 'Friday'),
(SAT, 'Saturday'),
)
# loads choices from defined list
eligible_days = models.CharField(max_length=14,choices=DAYS_OF_WEEK_CHOICES,
blank=False, default='Saturday')
campground_id = models.SmallIntegerField()
stay_length = models.SmallIntegerField()
start_date = models.DateField()
end_date = models.DateField()
admin.py
from django.contrib import admin
from searcher.models import CampgroundQuery
from forms import CampgroundQueryAdminForm
class CampgroundQueryAdmin(admin.ModelAdmin):
form = CampgroundQueryAdminForm
admin.site.register(CampgroundQuery, CampgroundQueryAdmin)
forms.py
from django import forms
from django.contrib import admin
from searcher.models import CampgroundQuery
class CampgroundQueryAdminForm(forms.ModelForm):
class Meta:
model = CampgroundQuery
widgets = {
'eligible_days': forms.widgets.CheckboxSelectMultiple
}
I know this is an old question, but for those who wish to avoid using a ManyToManyField, there is a package to do this, django-multiselectfield, which is quick and easy to implement.
forms.py
from multiselectfield import MultiSelectFormField
class MyForm(forms.ModelForm):
my_field = MultiSelectFormField(choices=MyModel.MY_CHOICES)
models.py
from multiselectfield import MultiSelectField
class MyModel(models.Model):
MY_CHOICES = (
('a', "A good choice"),
...
('f', "A bad choice"),
)
my_field = MultiSelectField(choices=MY_CHOICES, max_length=11)
And that's it! It stores the keys of MY_CHOICES in a comma-separated string. Easy!
A ManyToManyField is the correct choice.
In theory you could create a compact representation, like a string field containing representations like "M,W,Th" or an integer that you'll set and interpret as seven binary bits, but that's all an immense amount of trouble to work with. ManyToManyFields are fine.
I want to allow the user to provide his availability for any of three timeslots on any of the 7 days of the week.
The brute-force way to do this would be to simply provide 21 checkboxes, so
MONDAY_AM = models.BooleanField()
MONDAY_NOON = models.BooleanField()
...
SUNDAY_PM = models.BooleanField()
However this seems silly. What if I want to recall all the mondays, or all the evenings? It just doesn't seem like it would scale well.
I would like a more sophisticated approach, ideally one that would allow me to register these models to Django's admin so that I could see each user's availability in a table form, such as
|Mo Tu We Th Fr Sa Su
AM | x x
NOON | x x x x
PM | x x x
Any help would be much appreciated (and yes, thus far Doodle vastly outstrips the functionality of my thing, but 1. it's a learning project, 2. i plan to take it in a different direction)
edit: BTW, coming from Python and Qt and whatnot, my initial approach was nested for loops and dictionary, but South and Django seemed not to like that. I need to think more "database-like", so don't be afraid to submit something that's almost trivial. It's probably what I need, really.
In past I was also required this feature. In which Teacher has to define its availability timing in any of the day, so I came up with this model:
class Availability(models.Model):
WEEKDAY_CHOICES = (
(0, 'Monday'),
(1, 'Tuesday'),
(2, 'Wednesday'),
(3, 'Thursday'),
(4, 'Friday'),
(5, 'Saturday'),
(6, 'Sunday'),
)
id = models.AutoField(primary_key=True)
teacher = models.ForeignKey(Teacher)
weekday = models.PositiveSmallIntegerField(choices=WEEKDAY_CHOICES)
start_time = models.TimeField(null=True, blank=True)
end_time = models.TimeField(null=True, blank=True)
class Meta:
db_table = 'availability'
unique_together = ("teacher", "weekday")
I hope this will help you to get somewhere.