django-generic-views

ManyToManyField in Class Based Generic Views and Save Override

我只是一个虾纸丫 提交于 2021-02-11 07:51:55
问题 We have a class-based-generic view that adds an object with a ManyToManyField relationship. We're trying to modify some values on save() override for the relationships created on that ManyToManyField, but it doesn't seem like they ever get created in save(), so we're forced to call a post_save method in get_success_url so the relationships are created. This seems very, very ugly. How do we force the relationships to be created before we leave save()? I've seen this answer on SO, which says to

Save formset in an UpdateView

泪湿孤枕 提交于 2021-01-01 06:45:06
问题 I have the models Entry and Meaning below with many-to-many relation. I need to create an update form/view to edit Entry and Meaning objects at the same time. I have also to be able to add more Meaning objects to an Entry object while editing it. I tried to use UpdateView and modelformset_factory as below. I can see the forms, but my view doesn't save the Meaning changes. I have two questions: How do I save the Meaning changes? How do I add or delete a Meaning object in this form/view? Models

How to use Django Sessions Framework in generic views to send data to another

和自甴很熟 提交于 2020-08-10 19:19:27
问题 In my Django application, I am trying to use sessions to store a variable in my class view and later use it to send it to a function view. In the past I have used sessions on function views and now I am trying to use it on a class view. class someView(CreateView): form_class = myForm template_name = 'myTemplate.html' ... ... def get_context_data(self, **kwargs): request.session['my_var'] = '1234' return context Than I am trying to capture the session variable in another view like this: def

How to use Django Sessions Framework in generic views to send data to another

爱⌒轻易说出口 提交于 2020-08-10 19:18:57
问题 In my Django application, I am trying to use sessions to store a variable in my class view and later use it to send it to a function view. In the past I have used sessions on function views and now I am trying to use it on a class view. class someView(CreateView): form_class = myForm template_name = 'myTemplate.html' ... ... def get_context_data(self, **kwargs): request.session['my_var'] = '1234' return context Than I am trying to capture the session variable in another view like this: def

how overwrite Response class in django rest framework ( DRF )?

时光总嘲笑我的痴心妄想 提交于 2020-02-21 11:14:52
问题 I want to overwrite Response class of django rest framework so that response back responsive dictionary contain three parameter message , status and data Hello dears all I try to change Response Class in DRF to pass two extra parameter ( message, status ) plus data provide by DRF serializer. message pass the message like Done , User Created or etc and status pass the message like fail or success or etc message and this message useful for reserve special code between frontend and backend. I

how overwrite Response class in django rest framework ( DRF )?

可紊 提交于 2020-02-21 11:14:20
问题 I want to overwrite Response class of django rest framework so that response back responsive dictionary contain three parameter message , status and data Hello dears all I try to change Response Class in DRF to pass two extra parameter ( message, status ) plus data provide by DRF serializer. message pass the message like Done , User Created or etc and status pass the message like fail or success or etc message and this message useful for reserve special code between frontend and backend. I

Django UpdateView: Object to update has no values

一笑奈何 提交于 2020-01-07 05:28:07
问题 I'd like to update an object with generic view updateview. The problem arises when I edit an object. Instead of reaching a prefilled form, I reach a blank form. My template for this: {% extends 'base.html' %} {% block content %} <div class="container"> <form method="post"> {% csrf_token %} {{ form.as_p }} <input class="btn btn-danger" type="submit" value="Update" /> <a href="{% url 'display_targetbehavior' %}" class="btn btn-default">Take me back</a> </form> </div> {% endblock %} In my views

generic views: object_list how to pass request variable

孤街醉人 提交于 2020-01-05 18:58:50
问题 How to pass request variable in generic views to a queryset. For example i need to pass req_brand_slug from request to a filter in queryset: all_by_brand = { 'queryset': Br.objects.filter(slug=req_brand_slug) } url(r'^model/(?P<req_brand_slug>[\w|-]+)/$', all_by_brand , name='brand'), 回答1: You'll have to create your own view which calls the generic view with custom params. from django.views.generic.list_detail import object_list def my_view(request, req_brand_slug): extra_context = {} return

Django CreateView: set user before validation

删除回忆录丶 提交于 2020-01-02 10:02:05
问题 I have a model that uses different validation for its name field depending on whether the object was created by a user or by the system. class Symbol(models.Model): name = models.CharField(_('name'), unique=True, max_length=64) creator = models.ForeignKey('User', null=True, on_delete=models.CASCADE) def is_system_internal(self): """ whether or not this Symbol belongs to the system rather than having been created by a user """ return (self.creator is None) def clean(self): """ ensure that the

post method in generic class based view is not called upon form submission in Django?

China☆狼群 提交于 2019-12-31 04:54:11
问题 I have a written a mixin that overrides the POST and get_from_kwargs of CreateView . I am doing AJAX submission of my form. I see that get_from_kwargs is called by printing on the console. But none of the other methods such as post , form_valid or form_invalid is being called. I have placed print statements in these methods but none of them is being called. Here is my mixin: class PendFormMixin(object): form_hash_name = 'form_hash' pend_button_name = 'pend' def get_form_kwargs(self): """