I post this because I could not find an answer/example that satisfied me and I solved it a way I haven't read in there. Maybe it can be useful or can be discussed.
Basically, my app displays formsets where users can create objects. Once validated, the formset is displayed again and user can add new objects.
Specifying a day is not relevant.
I first started to use some javascript to do it, then played with Django custom model (to subclass models.DateField).
I could not succeed to do it with the later (my post updated linked to this one)
As I have deadlines, I did it with simple ChoiceField storing objects as Integers and then, use a #property method to build a proper date out of those user inputs (add 1 as day).
This way I avoided to deep dive in Django steam pipes and even got rid of a datepicker!
But thanks to the #property, I can still keep the convenience of formatting dates in Django templates and make date calculation etc...
models.py :
NOTE : make use to return None if the date cannot be built out of filled fields, otherwise you'll have an unbound error when trying to render those #property field in a template.
class MyModel(models.Model):
YEAR_CHOICES = [(y,y) for y in range(1968, datetime.date.today().year+1)]
MONTH_CHOICE = [(m,m) for m in range(1,13)]
start_year = models.IntegerField(choices=YEAR_CHOICES,
default=datetime.datetime.now().year,)
start_month = models.IntegerField(choices=MONTH_CHOICE,
default=datetime.datetime.now().month,)
end_year = models.IntegerField(choices=YEAR_CHOICES,
default=datetime.datetime.now().year,)
end_month = models.IntegerField(choices=MONTH_CHOICE,
default=datetime.datetime.now().month,)
#property
def start_date(self):
if self.start_year and self.start_month:
return datetime.date(self.start_year, self.start_month, 1)
elif self.start_year:
return datetime.date(self.start_year, 12, 31)
else:
return None
#property
def end_date(self):
if self.end_year and self.end_month:
return datetime.date(self.end_year, self.end_month, 1)
elif self.end_year:
return datetime.date(self.end_year, 12, 31)
else:
return None
forms.py
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
exclude = ("blabla",)
Now, if you want to display a date in a form template, you can use :
{{ form.instance.start_date }}
Hope it helps & Suggestions are welcome!
it was not too clear for me, what are you doing exactly?
if you want to show a date string which has only years and month. that would be achieved by using template-filter date and a format e.g: {{ obj.created_at|date:'%y %m' }}.
but if I'm wrong and may you want to have a custom filed in your modelForm to pick a date(which only has year and month) then something like you've already did is the idea, but the code is the matter
I think your start_date and end_date function need to be changed:
def start_date(self):
if self.start_year and self.start_month:
return datetime.date(self.start_year, self.start_month, 1)
elif self.start_year:
# return Jan 1st
return datetime.date(self.start_year, 1, 1)
else:
return None
def end_date(self):
if self.end_year and self.end_month:
if self.end_month == 12:
# if month is December, return December 31st
return datetime.date(self.end_year, self.end_month, 31)
else:
# else, get first day of next month and subtract 1 day.
return datetime.date(self.end_year, self.end_month+1, 1) - datetime.timedelta(days=1)
elif self.end_year:
return datetime.date(self.end_year, 12, 31)
else:
return None
Related
---------------- EDIT --------------------
I could not succed doing it with Custom Model Field and I had to move on, so actually, I did it the alternative way specified at the end of this post.
Here is a link to a new post exposing the solution.
---------------- END EDIT --------------------
My app displays formsets where users can create objects. Once validated, the formset is displayed again and user can add new objects.
Dates should only be month and year ("%m/%Y").
I could work on the input field (though jquery) to add '01/' in front of the date entered. But after submitting, the field now displays "%d/%m/%Y" (normal).
So, I'm looking for a way to translate input string (ex : 09/2018) to dateField to store in the database, and then a translate this dateField to string when datas are retrieved from database.
I know, I could simply use a form charfield for months and another one for years.
But I would like to keep it as a date object, so, in templates, I could perform date formatting ({{ edu.start_date |date:"D, d M, Y" }})
Django custom model fields sound made for this : Django custom model fields.
I could do something like this in the custom field:
def string_to_date(value):
value = '01/' + value
la_date = datetime.strptime(value, "%d/%m/%Y").date()
return la_date
def date_to_string(la_date_date):
la_date_str = la_date_date.strftime('%m/%Y')
return la_date_str
class DateYearMonth(models.DateField):
def get_prep_value(self, value):
if value is None:
return value
return string_to_date(value)
def to_python(self, value):
if value is None:
return value
return date_to_string(value)
The form associated (i commented widgets):
class EducationForm(forms.ModelForm):
start_date = forms.CharField()
end_date = forms.CharField()
class Meta:
model = Education
exclude = ("curriculum",)
# # les widgets :
# widgets = {
# 'start_date': forms.TextInput(),
# 'end_date': forms.TextInput(),
# }
Well, this does not work so far. But I don't even know if I'm heading to the right direction...
EDIT
Actually, maybe I could use simple charfield for month and year and add a model method that would create a date out of it (maybe a #property)... This way, I would keep it simple on the form and be able to format on the templates...
You can make the Year an IntegerField and Month (CharField or Integer) and store Year in Months individually is probably the best solution.
Below is an example fore Year only(kind of DatetimeField to YearField)
import datetime
YEAR_CHOICES = []
for r in range(1980, (datetime.datetime.now().year+1)):
YEAR_CHOICES.append((r,r))
year = models.IntegerField(_('year'), choices=YEAR_CHOICES,
default=datetime.datetime.now().year)
I have a list of client records in my database. Every year, we generate a single work order for each client. Then, for each work order record, the user should be able to create a note that is specific to the work order. However, not all work orders need a note, just some.
Now, I can't simply add a note field to the work order because some times, we need to create the note before the work order is even generated. Sometimes this note is specific to a work order that won't happen for 2-3 years. Thus, the notes and the work order must be independent, although they will "find" each other when they both exist.
OK, so here's the situation. I want the user to be able to fill out a very simple note form, where they have two fields: noteYear and note. Thus, all they do is pick a year, and then write the note. The kicker is that the user should not be able to create two notes for the same year for the same client.
What I'm trying to get as is validating the note by ensuring that there isn't already a note for that year for that client. I'm assuming this would be achieved by a custom is_valid method within the form, but I can't figure out how to go about doing that.
This is what I tried so far (note that I know it's wrong, it doesn't work, but it's my attempt so far):
Note that systemID is my client record
My model:
class su_note(models.Model):
YEAR_CHOICES = (
('2013', 2013),
('2014', 2014),
('2015', 2015),
('2016', 2016),
('2017', 2017),
('2018', 2018),
('2019', 2019),
('2020', 2020),
('2021', 2021),
('2022', 2022),
('2023', 2023),
)
noteYear = models.CharField(choices = YEAR_CHOICES, max_length = 4, verbose_name = 'Relevant Year')
systemID = models.ForeignKey(System, verbose_name = 'System ID')
note = models.TextField(verbose_name = "Note")
def __unicode__(self):
return u'%s | %s | %s' % (self.systemID.systemID, self.noteYear, self.noteType)
And my form:
class SU_Note_Form(ModelForm):
class Meta:
model = su_note
fields = ('noteYear', 'noteType', 'note')
def is_valid(self):
valid = super (SU_Note_Form, self).is_valid()
#If it is not valid, we're done -- send it back to the user to correct errors
if not valid:
return valid
# now to check that there is only one record of SU for the system
sysID = self.cleaned_data['systemID']
sysID = sysID.systemID
snotes = su_note.objects.filter(noteYear = self.cleaned_data['noteYear'])
for s in snotes:
if s.systemID == self.systemID:
self._errors['Validation_Error'] = 'There is already a startup note for this year'
return False
return True
EDIT -- Here's my solution (thanks to janos for sending me in the right direction)
My final form looks like this:
class SU_Note_Form(ModelForm):
class Meta:
model = su_note
fields = ('systemID', 'noteYear', 'noteType', 'note')
def clean(self):
cleaned_data = super(SU_Note_Form, self).clean()
sysID = cleaned_data['systemID']
sysID = sysID.systemID
try:
s = su_note.objects.get(noteYear = cleaned_data['noteYear'], systemID__systemID = sysID)
print(s)
self.errors['noteYear'] = "There is already a note for this year."
except:
pass
return cleaned_data
For anyone else looking at this code, the only confusing part is the line that has: sysID = sysID.systemID. The systemID is actually a field of another model - even though systemID is also a field of this model -- poor design, probably, but it works.
See this page in the Django docs:
https://docs.djangoproject.com/en/dev/ref/forms/validation/
Since your validation logic depends on two fields (the year and the systemID), you need to implement this using a custom cleaning method on the form, for example:
def clean(self):
cleaned_data = super(SU_Note_Form, self).clean()
sysID = cleaned_data['systemID']
sysID = sysID.systemID
try:
su_note.objects.get(noteYear=cleaned_data['noteYear'], systemID=systemID)
raise forms.ValidationError('There is already a startup note for this year')
except su_note.DoesNotExist:
pass
# Always return the full collection of cleaned data.
return cleaned_data
I'm using Django's generic year archive view to display event objects by year. This may or may not be the best way to do this since I found that Django restricts the object list to the year being passed; my date range spans the current year into the next.
Here's my view:
class VisitingScholarsYearView(YearArchiveView):
allow_empty = True
allow_future = True
date_field = 'event_date'
template_name = "events/dvs_test.html"
context_object_name = 'event_list'
make_object_list = True
def get_queryset(self):
return Event.school_year_events.from_year(self.get_year()).filter(event_type__title='Distinguished Visiting Scholars Series')
Here's the manager on my model (an Event object with a DateField called event_date):
class SchoolYearManager(models.Manager):
def live_events(self, start_date, end_date):
return self.filter(status=self.model.LIVE).filter(event_date__range=(start_date, end_date))
def this_year(self):
now = datetime.datetime.now()
current_year = now.year
start_date = datetime.date(current_year, 7, 1)
end_date = datetime.date((current_year + 1), 6, 30)
return self.live_events(start_date, end_date)
def from_year(self, year):
start_date = datetime.date(int(year), 7, 1)
end_date = datetime.date((int(year) + 1), 6, 30)
return self.live_events(start_date, end_date)
And finally, my url for the view:
url(r'^distinguished-visiting-scholars-series/(?P<year>\d{4})/$', VisitingScholarsYearView.as_view()),
When I hit the API, I get the events I expect. But the YearArchiveView appears to limit the returned events to the year I give it; this is also expected, but I'd like it to span the range I refer to in the manager (ie July 1 to June 30 ).
How can I change this behavior? Or should I attempt a different view (ListView)?
I don't think you should use YearArchiveView as the base here - there's too much built-in logic around getting the objects for that date.
Instead, use ListView with YearMixin:
class VisitingScholarsYearView(YearMixin, ListView):
Update: Reading directly the django source code i got one undocumented missing piece to solve my problem. Thanks to Brandon that solved half of the problem by giving me one of the missing pieces. See my own answer to see my solution (i dont want to mix things here).
I have the following (simplified) models:
Order(models.Model):
status = models.CharField( max_length=25, choices=STATUS_CHOICES, default='PENDING')
total = models.DecimalField( max_digits=22, decimal_places=2)
def clean(self):
if self.estatus == 'PAID' or self.estatus == 'SENT':
if len(self.payment.all()) > 0:
raise ValidationError("The status cannot be SENT or PAID if there is no payment for the order")
Payment(models.Model):
amount = models.DecimalField( max_digits=22, decimal_places=2 )
order = models.ForeignKey(Order, related_name="payment")
def clean(self):
if self.amount < self.order.total or self.amount <= 0:
ValidationError("The payment cannot be less than the order total")
In my admin.py i have:
class paymentInline(admin.StackedInline):
model = Payment
max_num = 1
class OrderAdmin(admin.ModelAdmin):
model = Order
inlines = [ paymentInline, ]
The validation in the clean method of the Order does not work because there is no payment saved when the validation occurs (obviously it has not been saved to the database).
The validation inside the payment works fine (if editing or adding a new payment).
I want to validate if the order has a payment if the status is 'PAID' or 'SENT', but as i cannot doit the way is in the clean method.
My question is, how can i access the 'payment.amount' value entered by the user in the inline (payment) of the Order form, to accomplish my validation? (considering im in the clean method of the Order model)
After reading the django source code i found one property of the BaseInlineFormSet that contains the Parent Instance of the Inline, in my case, the Order instance being edited.
Brandon gave me another important piece, iterating over the self.forms of the BaseInlineFormSet to get each of the instances (even not saved or not cleaned or empty), in my case, each Payment Instance being edited.
These are the two pieces of information needed to check if the Order with status 'PAID' or 'SENT' has a payment or not. Iterate over the cleaned_data of the formset would not give Order data (i.e. when not changing the Order, just changing the Payment, or when not adding a Payment -and an empty Payment- but changing the Order) which is needed to decide to save the model if the order status is different than 'PAID' or 'SENT', so this method was discarded before.
The models are keep the same, I only modified the admin.py to add the next:
class PaymentInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
order = None
payment = None
if any(self.errors):
return
# django/forms/models.py Line # 672,674 .. class BaseInlineFormSet(BaseModelFormSet) . Using Django 1.3
order = self.instance
#There should be only one form in the paymentInline (by design), so in the iteration below we end with a valid payment data.
if len(self.forms) > 1:
raise forms.ValidationError(u'Only one payment per order allowed.')
for f in self.forms:
payment = f.save(commit=False)
if payment.amount == None:
payment.amount = 0
if order != None:
if order.status in ['PAID', 'SENT'] and payment.amount <= 0:
raise forms.ValidationError(u'The order with %s status must have an associated payment.'%order.status)
class pymentInline(admin.StackedInline):
model = Payment
max_num = 1
formset = PaymentInlineFormset
class OrderAdmin(admin.ModelAdmin):
inlines = [ paymentInline, ]
admin.site.register(Order, OrderAdmin)
admin.site.register(Payment)
It sounds like you just need to validate that there is at least one valid formset in the inlines...you might give this code a try: http://wadofstuff.blogspot.com/2009/08/requiring-at-least-one-inline-formset.html
Hope that gets you going.
[Edit]
I took a look at some code I had written for another app that had custom validation on inlines based on a property on the related model. Of course, you may need to make some adjustments, but give this a try. You'll need to specify an inline to use in your admin model too.
#I would put this in your app's admin.py
class PaymentInline(admin.TabularInline):
model = Payment
formset = PaymentInlineFormset
#I would put this in the app's forms.py
class PaymentInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
order = None
valid_forms = 0
for error in self.errors:
if error:
return
for cleaned_data in self.cleaned_data:
amount = cleaned_data.get('amount', 0)
if order == None:
order = cleaned_data.get('order')
if amount > 0:
valid_forms += 1
if order.status in ['PAID', 'SENT'] and len(valid_forms) > 0:
raise forms.ValidationError(u'Your error message')
I am trying to access data.get_age_display in my email template. I can't seem to get the display of this. I am not sure what I am doing wrong, I've using get_FIELD_display numerous times before but passed as context to a normal template. Is there something different with forms?
class RequestForm(forms.Form):
ADULT = 1
SENIOR = 2
STUDENT = 3
AGE_GROUP = (
(ADULT, 'Adult'),
(SENIOR, 'Senior'),
(STUDENT, 'Student'),
)
name = forms.CharField(max_length=255)
phone = forms.CharField(max_length=15)
age = forms.ChoiceField(choices=AGE_GROUP)
details = forms.CharField(widget=forms.Textarea())
def save(self):
order = Order(
name = self.cleaned_data['name'],
phone = self.cleaned_data['phone'],
age = self.cleaned_data['age'],
details = self.cleaned_data['details'],
)
order.save()
template = loader.get_template('request_email.txt')
# send over the order object in an email extracted so they can handle the ticket order
context = Context({
'data': order,
})
#import pdb; pdb.set_trace()
email_subject = 'Request Tickets'
mail_managers(email_subject, template.render(context))
in my request_email.txt all I am doing is {{ data.get_age_display }} any ideas?
Jeff
You haven't shown the code for the Order model that you're creating. Are you sure that the age field on the model has choices set?
Any reason you're not using a ModelForm? You're creating an Order object within the form's save() method, but not returning it. A modelform would do that for you, as well as removing the need to redeclare the fields for the form.
I know this is coming WAAAAAY later than the question being posted but here's my answer for completeness and anyone else who might benefit from it :-)
I'm going to assume that in AGE_GROUP, ADULT, SENIOR and STUDENT are integers. Your form cleaning will NOT automatically clean the string contained in the POST and return an integer. So in this code:
context = Context({
'data': order,
})
you would think order.age is referring to an integer but that is, in fact, incorrect. It's burned me a few times before because this will correctly save the integer to the physical table, but the order instance still has the string representation of the age field.
You could do one of two things:
1. Clean this in the field:
clean_age(self):
return int(self.cleaned_data['age'])
or create a new field type:
def MyChoiceField(forms.ChoiceField):
def clean(self, value):
if not value:
if self.required:
raise forms.ValidationError(self.error_messages['required'])
return None
else:
return None
return int(value)
link that to the form field:
age = MyChoiceField(choices=AGE_GROUP)
and then you'll be able to apply this logic to any other such choice field in future. Personally, I find the latter approach the best one and I stick all my custom field types into a form_utils file so that I can use them everywhere. Another gotcha is that forms.charField doesn't automatically strip the entered text and you can use this approach to fix that too.