-
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
ListField containing ReferenceField's #55
Comments
To be honest I don't really know what you mean by "data in the same format as a I do know however that in the admin you can use the If you want to use any other widget, you should be fine if you continue to use the In general the Hope that helps. |
By "data in the same format as a We have reused some of our jQuery and are generating Thanks, |
Yeah there should be a way to do that. What you want to do is use a So, your form class should look like this. You will need the newest mongodbforms commit though, because until a few minutes ago we only allowed field classes to be passed, not objects. Note: This seems to be buggy. Just though I'd let you know how it's supposed to work (and will work soon). class ReferenceForm(DocumentForm):
references = ListField(ReferenceField(queryset=your_queryset))
... You should also be able to copy the first generated select if you want to insert more references with jQuery. Just make sure that you increment the "counter" in the id and name attribute in the select. And validation is probably quite inefficient because every ReferenceField will run db queries to se if it's valid. If that is a problem, you will need to write a more specialised field (which should be doable if you combine the |
Okay, so using the following document and form I get something that should be what you need. This is actual test code I use, so it has some attributes just for testing stuff. # documents.py
class Author(Document):
name = StringField(min_length=2)
def __unicode__(self):
return self.name
class Book(Document):
title = StringField(max_length=200, required=True, verbose_name="title", unique_with=['authors', 'language'])
authors = ListField(ReferenceField(Author, dbref=False, reverse_delete_rule=CASCADE), required=True, verbose_name="authors")
language = StringField(max_length=2, required=True, choices=LANGUAGES, verbose_name="language")
# forms.py
from mongodbforms import ListField, ReferenceField
from .documents import Book, Author
class BookForm(DocumentForm):
authors = ListField(ReferenceField(queryset=Author.objects()))
class Meta:
document = Book |
Thanks a lot. This seems to be working great as far as saving the posted variables. Were you envisioning the Edit: I was replying to your previous message. Just trying this out now |
I'm not sure I understand what you mean. But if you want to auto generate this from documents (and not provide a form for every # fieldgenerator.py
class MyFieldGenerator(MongoDefaultFormFieldGenerator):
def generate_listfield(self, field, **kwargs):
field = super(MyFieldGenerator, self).generate_listfield(field, **kwargs)
if isinstance(field.field, MongoReferenceField):
field = ListField(ReferenceField(queryset=field.field.document_type.objects.clone())
return field
# settings.py
# set the fieldgeneretor for the whole application
MONGODBFORMS_FIELDGENERATOR = 'myproject.fieldgenerator.MyFieldGenerator' After that you should get lots of selects for every I hope I did mention that you need the newest version from github in order for this to work. Hope that helps and is what you asked for. |
The generator is a good idea although I've already got the form in place so keeping it in the form is fine for this project. One bug is that it throws
|
Oh. Yeah I didn't test that. The last commit should handle that. |
Yep, works like a charm. Thanks a lot for your help. If you're ever in the UK I owe you a beer or three. |
The current multiple choice widget for
ListField(ReferenceField(Foo))
doesn't provide a very good user experience and our team has written jQuery to create an interface that posts data in the same format as aListField(StringField())
.Our current processing of this data is far from optimal and I wonder how it should be approached?
Thanks,
Andrew
The text was updated successfully, but these errors were encountered: