These are chat archives for CharlesGust/django-imagr

15th
Nov 2014
Charlie
@charlieRode
Nov 15 2014 03:05
Got it, Ben. There were a couple of knots to untangle, and printing things to the console was paramount to finding them. When you upload file data, you must specify: <form enctype="multipart/form-data" ...>. I had misspelled 'multipart'.
Additionally, you must define MEDIA_ROOT in settings, and it's definition is a relative path (even though the docs say it's absolute...) from the root dir of the project. In this case, our root project dir is projects/django-imagr/django-imagr/imagr_site. So I set MEDIA_ROOT = 'imagr_app/static'.
Furthermore (this wasn't part of the problem, but was a tangential confusion,) when defining your models, each field can take an optional keyword-arg 'null=True', to allow null vals in the database EXCEPT when those fields are file types. In that case, 'blank=True' is the kwarg you need to pass to allow for empty fields.
Charlie
@charlieRode
Nov 15 2014 08:27

I finally was able to make the Album ModelForm work. Turns out that a M2M field can't be given data upon the instantiation of an instance. Rather, you need to create the instance (via the view function) and then assign relationships to that field, using the instance.fieldname.add(data) method syntax. Furthermore, since the .add() method only takes a single, non-iterable item for an argument, you need to add each photo one at a time. In our case, this all looks something like:

form = AlbumForm(request.POST)
if form.is_valid():
    new_album = Album.objects.create(user=<userObject>, title=form.cleaned_data['title']...other keyword arguments except photos...)
photos = form.cleaned_data['photos']
for photo in photos:
    new_album.photos.add(photo)
new_album.save()

Everything has been pushed to my branch of the repo. I'm really relieved that something as straightforward as injecting data into a M2M field doesn't require something as complicated as an overwrite of the ModelForm's ...init... function.

Charles Gust
@CharlesGust
Nov 15 2014 16:54
Thanks Charlie! My plan today is to come in for the technical interview training, maybe stay over for an hour of Git. Not sure about Sunday, but probably available if you folks are.