I have a model in my dJango app called event. I want users to be able to clone event to create a new one without having to start from scratch — not all the information changes everytime.
When I do this, as it loads the clone event page, I’m getting validation errors on some of the required fields since I’m not copying over all the information from the existing event, only some of the fields. The fields that are not copied and required are throwing validation errors when the page first loads (but the user hasn’t submitted the form yet). Is there a way to make that initial validation not happen?
[EDIT]
Model:
class Event(models.Model):
class Meta:
db_table = 'event'
type = models.ForeignKey(EventType, default="0")
title = models.CharField(max_length=150, default='Event')
location = models.TextField()
description = models.TextField(max_length=1050)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
View and Template:
In the case of clone, I get the event by the event_id to clone from by doing Event.objects.get(id=event_id). Once I get the object, I set the following value for the form by doing:
map = {
'type': event.type.name,
'title': event.title,
'location': event.location,
'description': event.description,
'start_time': event.start_time,
'end_time': event.end_time
}
form = EventForm(map)
The values in the form object are the same name as the ones in the model. In the case of clone, say I get rid of title from the map, and load it into the form, when I show the form on the template, it has a validation error under it…but the user just opened the page…
You can do it like so: