samedi 9 août 2014

Web - Visualisez le champ parent sur les formes de django - Stack Overflow


I'm developing a website based on Django. I'm creating forms now to be filled by the user.


First, I'm displaying a UserProfileForm to be filled out by the user, but I need to show the fields 'username', 'first_name' and 'last_name' that the User class have.


Second, I'm displaying a Form to create a Mascot Object, but I need to show an Image Field that it is inside of another Class called Image.


These are my classes(models.py):


class Mascot(models.Model):

name = models.CharField(max_length=50)
race = models.CharField(max_length=50)
birth_date = models.DateField(blank=True, null=True)

def __str__(self):
return self.name

class Image(models.Model):
mascot = models.ForeignKey(Mascot)
image = models.ImageField(upload_to = 'pic_folder/%Y/%m/%d', default = 'pic_folder/None/no-img.jpg')
is_profile_pic = models.BooleanField()

This is my form(forms.py):


class MascotForm(forms.ModelForm):

class Meta:
model = Mascot

So, I need to display on the MascotForm the Image object. Thanks.




You can simply add an ImageFormField to the MascotForm and override the save method to create the Image instance also, like this:


class MascotForm(forms.ModelForm):
image = forms.ImageField()

class Meta:
model = Mascot

def save(self, commit=True):
# mascot will have to save in this case, we don't pass commit
mascot = super(MascotForm, self).save()
image = Image.objects.create(mascot=mascot,
image=self.cleaned_data.get('image'))
return mascot

This would be one way. But a better way would be to have both forms in the view. Create a ImageForm for the Image model and handle the save in the view:


    class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ('image',)

def save(self, mascot, commit=True):
image = super(ImageForm, self).save(commit=False)
image.mascot = mascot
if commit:
image.save()
return image

def my_view(request):
if request.method == 'POST':
mascot_form = MascotForm(request.POST)
image_form = ImageForm(request.POST, request.FILES)

if mascot_form.is_valid() and image_form.is_valid():
mascot = mascot_form.save()
image = image_form.save(mascot)
return redirect(....)
else:
mascot_form = MascotForm()
image_form = ImageForm()
return render(request, 'some_template.html', {
'mascot_form': mascot_form,
'image_form': image_form
})


I'm developing a website based on Django. I'm creating forms now to be filled by the user.


First, I'm displaying a UserProfileForm to be filled out by the user, but I need to show the fields 'username', 'first_name' and 'last_name' that the User class have.


Second, I'm displaying a Form to create a Mascot Object, but I need to show an Image Field that it is inside of another Class called Image.


These are my classes(models.py):


class Mascot(models.Model):

name = models.CharField(max_length=50)
race = models.CharField(max_length=50)
birth_date = models.DateField(blank=True, null=True)

def __str__(self):
return self.name

class Image(models.Model):
mascot = models.ForeignKey(Mascot)
image = models.ImageField(upload_to = 'pic_folder/%Y/%m/%d', default = 'pic_folder/None/no-img.jpg')
is_profile_pic = models.BooleanField()

This is my form(forms.py):


class MascotForm(forms.ModelForm):

class Meta:
model = Mascot

So, I need to display on the MascotForm the Image object. Thanks.



You can simply add an ImageFormField to the MascotForm and override the save method to create the Image instance also, like this:


class MascotForm(forms.ModelForm):
image = forms.ImageField()

class Meta:
model = Mascot

def save(self, commit=True):
# mascot will have to save in this case, we don't pass commit
mascot = super(MascotForm, self).save()
image = Image.objects.create(mascot=mascot,
image=self.cleaned_data.get('image'))
return mascot

This would be one way. But a better way would be to have both forms in the view. Create a ImageForm for the Image model and handle the save in the view:


    class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ('image',)

def save(self, mascot, commit=True):
image = super(ImageForm, self).save(commit=False)
image.mascot = mascot
if commit:
image.save()
return image

def my_view(request):
if request.method == 'POST':
mascot_form = MascotForm(request.POST)
image_form = ImageForm(request.POST, request.FILES)

if mascot_form.is_valid() and image_form.is_valid():
mascot = mascot_form.save()
image = image_form.save(mascot)
return redirect(....)
else:
mascot_form = MascotForm()
image_form = ImageForm()
return render(request, 'some_template.html', {
'mascot_form': mascot_form,
'image_form': image_form
})

0 commentaires:

Enregistrer un commentaire