django-generic-views

Django CreateView: set user before validation

风流意气都作罢 提交于 2019-12-06 09:52:45
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 Symbol's name is valid """ if self.is_system_internal(): if not re.match("^_[a-zA-Z0-9\-_]+$", self

Django IntegrityError at /new null value in column “owner_id” violates not-null constraint

ぃ、小莉子 提交于 2019-12-05 13:45:26
I'm trying to track the user that created an object using a CreateView and I'm doing it exactly like it's done in documentation ( https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/ , Models and request.user) except I don't use login_required() decorator but LoginRequiredMixin from django-braces instead. My model: class Contact(models.Model): owner = models.ForeignKey(User, editable=False) first_name = models.CharField(max_length=255,) last_name = models.CharField(max_length=255,) email = models.EmailField() My view: class CreateContactView(LoginRequiredMixin,

Manually calling a class based generic view

*爱你&永不变心* 提交于 2019-12-05 09:55:13
问题 I'm currently trying to call a class based Generic view from within another class based generic view and cant seem to do it correctly. Ways I've tried: result = CategoryTypes.as_view() # The same way you put it in the urlconf print result Prints: <function CategoryTypes at 0x92bd924> CategoryTypes.as_view()(self.request) # & CategoryTypes().dispatch(self.request) Tracebacks: ContentNotRenderedError at /crm/categories/company/ The response content must be rendered before it can be accessed.

django how to loop through the context object passed back by a generic detailview?

試著忘記壹切 提交于 2019-12-04 18:14:07
I'm using a generic DetailView to display a project object. Can I loop through the fields somehow in my template or do I have to place every field. url(r'^(?P<slug>[-\w]+)/$', DetailView.as_view(model=Project, template_name='projects/detail_project.html',slug_field='slug', context_object_name='project'), name='project_detail'), I've got something like this in my template: {{ project.title }} {{ project.created_date }} etc... Is there a way to do something like this? <table> {% for field in project %} <tr> <td>{{ field }}</td> </tr> {% endfor %} </table> I tried the above snippet and got this

How does one use a custom widget with a generic UpdateView without having to redefine the entire form?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 00:54:55
I have a model with a ManyToMany relation that I would like to update with a CheckBoxSelectMultiple widget while everything else uses the default generic form, but when I redefine that one form field, it is the only one that shows up in the UpdateView. Is there a way to use a widget with just one field without having to redefine the entire form? Views.py: from django.views.generic.edit import UpdateView from kunden.models import Kunde, Unternehmenstyp from kunden.forms import KundeEditForm class KundeUpdate(UpdateView): model = Kunde form_class = KundeEditForm template_name = 'kunden/kunde

Manually calling a class based generic view

江枫思渺然 提交于 2019-12-03 23:58:53
I'm currently trying to call a class based Generic view from within another class based generic view and cant seem to do it correctly. Ways I've tried: result = CategoryTypes.as_view() # The same way you put it in the urlconf print result Prints: <function CategoryTypes at 0x92bd924> CategoryTypes.as_view()(self.request) # & CategoryTypes().dispatch(self.request) Tracebacks: ContentNotRenderedError at /crm/categories/company/ The response content must be rendered before it can be accessed. result = CategoryTypes().__init__() print result Prints: None How do I call this from another view? I've

How to set a field of the model in view using generic views?

心不动则不痛 提交于 2019-12-03 15:14:48
问题 I have a model, which has an author ForeignKey , as such: class Appointment(models.Model): # ... author = models.ForeignKey(User) I want the author field to be set automatically when creating appointment to currently logged-in user. In other words, the author field should not appear in my Form class: class AppointmentCreateForm(ModelForm): class Meta: model = Appointment exclude = ('author') There are two problems: How to access the form in generic CreateView and set the author ? How to tell

Extending generic view classes for common get_context_data

三世轮回 提交于 2019-12-03 09:00:24
问题 I constantly see myself having to add the same extra variable to the context of many of my views. def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(MyListView, self).get_context_data(**kwargs) # Add in the house context['house'] = self.get_object().house return context As I don't like repeating myself, I thought I could create a new class extending the view and then I could base all my views on the new extended view class. The thing is

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

久未见 提交于 2019-12-02 05:40:48
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): """ Returns a dictionary of arguments to pass into the form instantiation. If resuming a pended form, this will

Django: How to set a hidden field on a generic create view?

◇◆丶佛笑我妖孽 提交于 2019-12-01 06:40:54
I'm running Django 1.6.x To extend my user I've added another model storing the data: class UserProfile (models.Model): user = models.ForeignKey(User) height = models.IntegerField(blank=True, null=True) Now I wand to add a view, which is allowing the user to add its own infos there. Started with django.views.generic.edit.CreateView , but want also to provide at least the edit/update one to. So I've added the import and created a view: from django.views.generic.edit import CreateView, UpdateView # .... class UserProfileCreateView(CreateView): model = UserProfile fields = ['height'] Also I've