-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some examples #11
Comments
Are you referring to problems with uploaded files as on the mailing list? Or are there other problems? |
I'm talking (here) about the continuation of the code in the readme file. It says: I've really tried, but I can't seem to make this work. Thats why a complete example would be very helpful for people like me :) |
Okay, I'll give it a shot. Documentation isn't really my strength as you might have guessed :) Perhaps you could be a little more precise what you actually can't get to work until then? |
jejej ok, I'll try. What I want to do, are two things:
#models.py class Comment(EmbeddedDocument): #forms.py #views.py
#models.py #forms.py #views.py |
Oh, right. I'm not sure how much about you know about Django, so first a link to the Django book about working with forms: http://www.djangobook.com/en/2.0/chapter07/ For the concrete example: def upload_photo(request):
if request.method == 'POST':
# if you edit an existing file, get the edited model first and pass it in as first argument
form = AddPhotoWithCommentForm(request.POST, request.FILES)
if form.is_valid():
model = form.save() # the new model is already saved to the database
return HttpResponse # -> Usually a redirect somewhere or do something else. Django docs help you here
else:
form = AddPhotoWithCommentForm()
return render_to_response("some_template", {'tpl_form': form}) # -> Again, have a look at the Django docs for more options. Hope that helps. |
Awesome, when I make everything work, I'm going to pull-request an examples directory :) |
Hi, sorry, I have a problem with the EmbeddedDocumentForm. I have: models.pyclass Interview(Document): class Question(EmbeddedDocument): forms.pyclass QuestionForm(EmbeddedDocumentForm): How must be the constructor form?. I tried: ...but , how to pass the "request.POST"? Can you help me, please? |
The easiest way is not to use keyword arguments, but positional arguments:
or if you want to use keyword args:
In fact every argument except for
Hope that helps. |
The information was helpfuly, it's works!. Thank you very much |
Hi, In the prevoius example I pass a single question in request.POST : question="how are you?", answer="fine", it's works, but, how I pass a LIST of questions?? Can you help me, please? |
Hi, again... I found a method called embeddedformset_factory...to do the previous question ^ InterviewFormSet = embeddedformset_factory(document=InterviewQuestion, parent_document=Interview) data = { formset = InterviewFormSet(data, instance=InterviewQuestion()) if formset.is_valid(): .... I tried to do similar "formset_factory". But this dosen't works!!, (either "formset.errors") I hope you help me |
Hey,
To get the initial formset use:
To render use:
To save use:
Hope that helps... |
Hmm. embeddedformset_factory returns a django.forms.FomSet type which does not have an instance kwarg. I have the following: models.py class Page(me.EmbeddedDocument):
title = me.StringField(max_length=255)
help_text = me.StringField()
class LegalDocument(me.Document):
title = me.StringField(max_length=255)
description = me.StringField()
pages = me.SortedListField(me.EmbeddedDocumentField(Page)) forms.py from mongodbforms import DocumentForm, embeddedformset_factory
from .models import LegalDocument, Page
class LegalDocumentForm(DocumentForm):
class Meta:
document = LegalDocument
PageFormSet = embeddedformset_factory(Page, LegalDocument) views.py def document_edit(request, doc_id=None):
if doc_id:
doc = get_document_or_404(LegalDocument, id=doc_id)
else:
doc = LegalDocument()
if request.POST:
form = LegalDocumentForm(request.POST, instance=doc)
if form.is_valid():
form.save()
return HttpResponseRedirect("/doc") #FIXME
#not even attempting to save the formset yet
else:
form = LegalDocumentForm(instance=doc)
page_formset = PageFormSet(instance=doc)
return render(request, "doc/edit.html", dict(form=form, page_formset=page_formset)) And i get the following error
Am I missing something here? |
Yep, you are missing something. The argument to the embedded formset for the parent document is now called |
So i assume you mean changing PageFormSet(instance=doc) to page_formset = PageFormSet(parent_document=doc) After that I get an error that
Where do you set the embedded_field for a EmbeddedDocumentFormSet form. It right now is set to None. |
At the moment that can only be done with I admit that the only place I have used the embeddedformset factory is in the admin, so the whole API sucks in parts. Uhm, I think I'll get around to writing a patch tomorrow. |
Okay, the So far this seems to work for me, but a bit more testing never hurts so let me know if there are any problems with the patch. |
When we have CHOICES set for fields used inside the models in models.py, mongodb forms throws error. If I remove CHOICES from sex field, then it works properly. SEX = ('Male', 'Female') class UserInfo(User, DynamicDocument): address = EmbeddedDocumentField(Address) dob = DateTimeField(required = False) sex = StringField(choices = SEX) # primary_number = LongField(required = False) It gives the following error:
|
I think the choices need to look like described in the Django docs (that's at least what has been tested and works). See here https://docs.djangoproject.com/en/dev/ref/models/fields/#choices Something like SEX = (
('M', 'Male'),
('F', 'Female'),
) Should work. Or |
Thanks. Got this working. |
I can't figure out how to make this work :(
I feel weird asking for more help, but hey, I'd gladly add an example folder with a working app if someone provides me with the "views.py" for a working example.
Thanks!
The text was updated successfully, but these errors were encountered: