Reputation: 350
I have two functions in the same model , and I need to use them in my generic view , one function in a view and the other function in the other view .
class Container(models.Model):
vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE ,null=True)
.
.
state = models.CharField(max_length = 120)
Free_volume = models.FloatField()
def get_absolute_url(self):#the first function
return reverse('vehicule_app:container-view')
def get_absolute_url(self):#the secont function
return reverse('vehicule_app:details', kwargs={'pk' : self.pk})
I want to use the first function in this view :
form_class = ContainerForm
template_name = 'vehicule_app/container_form.html'
and I want to use the second function in other view :
class ContainerCreate(CreateView):
form_class = ContainerForm
template_name = 'vehicule_app/container_form.html'
Upvotes: 0
Views: 278
Reputation: 1633
Rename your function. They do not return the same choice. They do not need to have the same name.
def get_absolute_container_url(self):
return reverse('vehicule_app:container-view')
def get_absolute_details_url(self):
return reverse('vehicule_app:details', kwargs={'pk' : self.pk})
If you want only one function, you can add an optional parameter.
def get_absolute_url(self, get_details=False):
if get_details:
return reverse('vehicule_app:details', kwargs={'pk' : self.pk})
else:
return reverse('vehicule_app:container-view')
In your views:
class ContainerCreate(CreateView):
form_class = ContainerForm
template_name = 'vehicule_app/container_form.html'
def form_valid(self, form):
# This function is call when form is valid.
# Put your code here
Upvotes: 1