modelform

Access request.user in modelForm

折月煮酒 提交于 2019-12-01 11:33:17
If i am using model forms where my views.py looks like: def dog_image_upload(request): if request.user.is_authenticated(): if request.method == 'POST': form = DogImageForm(request.POST, request.FILES) if form.is_valid(): form.save() else: form = DogImageForm(user) return render_to_response("dog-image-upload.html", {'form': form}, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/') And in model.py i want to do this: class DogImageForm(ModelForm): dogs = forms.ModelChoiceField(queryset=Dog.objects.filter(user=request.user)) class Meta: model = ResultsUpload fields =

django update modelform

淺唱寂寞╮ 提交于 2019-12-01 01:58:22
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) contact = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) friends = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) location = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) My modelform is as follows class PrivacyForm(ModelForm): class Meta: model = UserPrivacy exclude = ('user','location') My function looks like this to display and update the form. def show_privacy(request): if not request.user.is

django update modelform

青春壹個敷衍的年華 提交于 2019-11-30 21:12:17
问题 I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) contact = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) friends = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) location = models.SmallIntegerField(default=1, choices=PRIVACY_TYPE) My modelform is as follows class PrivacyForm(ModelForm): class Meta: model = UserPrivacy exclude = ('user','location') My function

django modelformset_factory sustains the previously submitted data even after successfully created the objects

十年热恋 提交于 2019-11-30 10:31:19
I am using django modelformset_factory in one of my view. I am using javascript for adding new forms to the formset in Template. Everything is working fine but my problem is that when i try to create a new object using modelformset_factory it shows me all the objects as forms which i previously created. Or in simple words it shows me the last submitted forms when i create a new instance using modelformset_factory forms. Like I have defined extra = 0 while initializing the modelformset_factory and from template I added 5 forms to formset and submitted those. Next time when I will re render the

Django: saving multiple modelforms simultaneously (complex case)

☆樱花仙子☆ 提交于 2019-11-30 09:49:14
问题 Well, it may actually be a simple case but I'm having a tough time figuring it out. I have two user registration funnels (corporate & everyone else). When a corporate user creates a User instance through my registration form, I would also like them to input secondary forms that create related instances (based on the Website and Organization_Contact models). I know how to solve this by making additional synchronous or asynchronous requests, but I'd like the user to fill out the three forms and

Django ModelForm instance with custom queryset for a specific field

喜夏-厌秋 提交于 2019-11-30 03:01:57
I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on = models.DateField(auto_add_now=True) All users may work at a number of factories and therefore their user profiles all have a ManyToManyField to Factory . Now I want to construct a ModelForm for Bike but I want the made_at list to consist of only factories at which the current user works. The idea is that users should be able to add bikes that they've assembled and enter which of the factories the bike was made at. How do I do that? cethegeek You question might be a dupe of this .

django modelformset_factory sustains the previously submitted data even after successfully created the objects

老子叫甜甜 提交于 2019-11-29 15:46:17
问题 I am using django modelformset_factory in one of my view. I am using javascript for adding new forms to the formset in Template. Everything is working fine but my problem is that when i try to create a new object using modelformset_factory it shows me all the objects as forms which i previously created. Or in simple words it shows me the last submitted forms when i create a new instance using modelformset_factory forms. Like I have defined extra = 0 while initializing the modelformset_factory

Using class based generic view DetailView with a ModelForm reveals a bug - how to proceed?

本小妞迷上赌 提交于 2019-11-29 10:50:29
I've been impressed how rapidly a functional website can go together with generic views in the tutorials. Also, the workflow for form processing is nice. I used the ModelForm helper class to create a form from a model I made and was delighted to see that so much functionality came together. When I used the generic list_detail.object_detail I was disappointed that all that I could display were fields individually. I knew the ModelForm class contained information for rendering, so I wanted to use the ModelForm with a generic view. I was asking around on stackoverflow to get some direction, and

field choices() as queryset?

谁都会走 提交于 2019-11-28 09:15:40
I need to make a form, which have 1 select and 1 text input. Select must be taken from database. model looks like this: class Province(models.Model): name = models.CharField(max_length=30) slug = models.SlugField(max_length=30) def __unicode__(self): return self.name It's rows to this are added only by admin, but all users can see it in forms. I want to make a ModelForm from that. I made something like this: class ProvinceForm(ModelForm): class Meta: CHOICES = Province.objects.all() model = Province fields = ('name',) widgets = { 'name': Select(choices=CHOICES), } but it doesn't work. The

Dynamically update ModelForm's Meta class

和自甴很熟 提交于 2019-11-28 02:58:03
I am hoping to dynamically update a ModelForm's inline Meta class from my view. Although this code seems to update the exclude list in the Meta class, the output from as_p() , as_ul() , etc does not reflect the updated Meta exclude. I assume then that the html is generated when the ModelForm is created not when the as_*() is called. Is there a way to force the update of the HTML? Is this even the best way to do it? I just assumed this should work. Thoughts? from django.forms import ModelForm from testprogram.online_bookings.models import Passenger class PassengerInfoForm(ModelForm): def set