Comments (14)
[deleted comment]
from django-ajax-selects.
As a brief follow-up, I can see the "tag_id" are being sent in the POST, in the
format "|34|45|54|". Do I need to write a custom save method to deal with this
format? How is the admin dealing with it?
Original comment by [email protected]
on 17 Feb 2011 at 7:33
from django-ajax-selects.
I fixed this with a few lines added to my "add_question" view. I ended up doing
.save() with the new_question twice in order for it to work, because I was
getting an error saying the new Question model instance had to have it's
primary key defined prior to having many-to-many field values set. I guess that
means I have to save it; I want to use the usual auto-incrementing id for my
pk.
I'm pretty sure I'm doing this in a very inefficient, hacky manner, and any
suggestions to make the code better would be much appreciated.
views.py, updated:
def add_question(request, template_name = 'quiz/add_question.html', *args,
**kw):
if request.method == "POST":
request_post = request.POST.copy()
question_form = QuestionForm(data=request_post,files=request.FILES)
if question_form.is_valid():
new_question = question_form.save(commit=False)
new_question.save()
##### update
tag_ids = question_form.cleaned_data['tags']
for tag_id in tag_ids:
tag = Tag.objects.get(id=tag_id)
new_question.tags.add(tag)
new_question.save()
##### update_end
else:
question_form = QuestionForm()
return render_to_response(template_name, {'title': _("Add a question"),
'form' : question_form}, context_instance=RequestContext(request))
Original comment by [email protected]
on 17 Feb 2011 at 9:05
from django-ajax-selects.
sorry, I was out and about in the world.
I'll check it out and send an answer soon.
(it certainly works outside the admin, i usually use it that way)
Original comment by [email protected]
on 17 Feb 2011 at 9:25
- Added labels: Type-Other
- Removed labels: Type-Defect
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
[deleted comment]
from django-ajax-selects.
This exact issue well described in the Django docs:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
From Django docs: "" Another side effect of using commit=False is seen when
your model has a many-to-many relation with another model. If your model has a
many-to-many relation and you specify commit=False when you save a form, Django
cannot immediately save the form data for the many-to-many relation. This is
because it isn't possible to save many-to-many data for an instance until the
instance exists in the database.
To work around this problem, every time you save a form using commit=False,
Django adds a save_m2m() method to your ModelForm subclass. After you've
manually saved the instance produced by the form, you can invoke save_m2m() to
save the many-to-many form data. ""
So, in my first post, I'm using .save(commit=False) for no reason other than to
raise another frivolous 'issue' about your plugin. On realizing today that I'd
like to save the request.user as the Question owner when they submit the form,
now I'll make use of commit=False and use the suggested save_m2m() method
afterwards.
views.py snippet:
if question_form.is_valid():
new_question = question_form.save(commit=False)
# owner is a ForeignKey to a User object
new_question.owner = request.user
new_question.save()
question_form.save_m2m()
Original comment by [email protected]
on 19 Feb 2011 at 9:31
from django-ajax-selects.
yes, I think you've found it. IMO django is a bit of a juggler when it comes to
the forms. you have to keep track of stuff and know the internals.
is your issue fully solved now ? sorry I didn't get a chance to take a look
Original comment by [email protected]
on 19 Feb 2011 at 9:53
from django-ajax-selects.
Yes, thank you.
Original comment by [email protected]
on 19 Feb 2011 at 11:20
from django-ajax-selects.
Original comment by [email protected]
on 23 Jul 2011 at 7:44
- Changed state: Done
from django-ajax-selects.
Related Issues (20)
- Unclear documentation for Add Object popup and link HOT 2
- Channel customisation to hide input on select HOT 1
- Unable to use values() in get_query method HOT 6
- JavaScript needs to be quoted HOT 7
- Initial value causes error. HOT 2
- Empty help string will display gettext header HOT 5
- Caught ValueError while rendering: invalid literal for int() with base 10: HOT 2
- Retain text in the input box HOT 5
- Show form field without a fk HOT 3
- Trigger only after 'n' characters HOT 1
- The ajax_lookup view should return a 'text/plain' Content-Type HOT 3
- Invalid markup HOT 3
- Doc suggestion: add a class Meta on ContactMailingForm HOT 1
- [patch] Field with name 'media' clashes with ModelForm.media
- JS incorrectly calculating the locations.. HOT 2
- Incorrect location of static files HOT 5
- make_ajax_field method overlooks initial value HOT 2
- {{ field.contents }}should render the display version of a field HOT 1
- tag the git source code HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ajax-selects.