Note: Using django-crispy-forms
library for my form. If you have a solution to my problem that involves not using the cripsy_forms
library, I accept it all the same. Not trying to be picky just need a solution / work around. Thanks
In my form's Meta
class I set the model, Driftwood
, and it's fields I want to in the form, but I also want to add another field. One that does not belong the the referenced model. This field I want to add is an image. The reason for this field is to build another model from it.
I have a model named Image
that has some fields that get populated by doing things with a single models.ImageField()
. This Image
also has a models.ForeginKey()
with a relation to the Driftwood
model. So Image
can be accessed through an instance of a Driftwood
using its relational set property (driftwood.image_set
).
In view.py
I am using generic.CreateView()
as the inherited class that will handle my form class. I plan on using the form_valid()
method to acquire through form.cleaned_data
, the image that I need. I will then create the image, passing the object.id
of my newly instantiated Driftwood
and the image into my Image
model.
The problem I have though is not knowing how to add a custom field to Django's FormModel
that does not belong to the model associated with the form.
forms.py
from django import formsfrom crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, ButtonHolder, Submitfrom . import modelsclass DriftwoodForm(forms.ModelForm):class Meta:model = models.Driftwoodfields = ('user', 'title', 'description')def __init__(self, *args, **kwargs):super(DriftwoodForm, self).__init__(*args, **kwargs)self.helper = FormHelper()self.helper.layout = Layout('user','Insert Image Field Here','title','description',ButtonHolder(Submit('save', 'Save', css_class='btn-success')))
models.py
from base64 import b64decode, b64encode # used to encode/decode imagefrom django.db import modelsclass TimeStampMixin(models.Model):class Meta:abstract = Truecreated = models.DateTimeField(auto_now_add=True)modified = models.DateTimeField(auto_now=True)class Driftwood(TimeStampMixin):user = models.ForeignKey(User)title = models.CharField(max_length=255)description = models.TextField(max_length=1000)slug = models.SlugField(max_length=255)class Image(TimeStampMixin):driftwood = models.ForeignKey(Driftwood)image = models.ImageField(upload_to='static/images')# gets encoded as a string in the save methodencoded_image = models.TextField(blank=True, null=False, default='')