I need your help, because I'm stuck in a project with no idea about how to continue.
I need to implement a simple "generic" document management app to integrate it in my project. I can have many kind of documents that are defined by "utilisation_type" in "DocumentType" model.
At the end, I need to display a page with an inline form, with only one of each "DocumentType" with (in this case) "utilisation_type"= "USER".
Like this, the user will get, for exemple, a page with 3 buttons, each button corresponding to only one DocumentType with "utilisation_type"= "USER" and he will be able to upload only one of each requested document.
Here are my models :
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
UTILISATION_TYPE_CHOICES = (
("USER", _(u"User")),
("PROJECT", _(u"Project")),
("TEAM", _(u"Team")),
("ALL", _(u"All")),
)
class DocumentType(models.Model):
document_type = models.CharField(max_length=40, choices=DOCUMENT_TYPE_CHOICES)
title = models.CharField(_(u'Titre'), max_length=255, blank=False, null=True)
utilisation_type = models.CharField(max_length=15, choices=UTILISATION_TYPE_CHOICES)
class Document(models.Model):
document_type = models.ForeignKey(DocumentType)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
document = models.FileField(_(u"Document"), upload_to='upload/documents', null=True, blank=True)
class MyUserManager(BaseUserManager):
first_name = models.CharField(_(u'Firstname'), max_length=90, blank=False)
last_name = models.CharField(_(u'Lastname'), max_length=90, blank=False)
Here is my view :
@login_required
def documents(request):
"""
User's documents edition
"""
user = request.user
DocumentInlineFormSet = generic_inlineformset_factory(Document)
if request.method == 'POST':
formset = DocumentInlineFormSet(request.POST, request.FILES, instance=user)
if formset.is_valid():
formset.save()
messages.success(request, _(u'Profil updated'), fail_silently=True)
return redirect('users.views.documents')
else:
formset = DocumentInlineFormSet(instance=user)
return render_to_response('users/user_documents.html', {'formEditDocumentFormset': formset,}, context_instance=RequestContext(request))
I hope I've been clear enough in my explanation. Do not hesitate to ask more details if needed.