Coder Social home page Coder Social logo

Comments (14)

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
[deleted comment]

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024
Yes, thank you.

Original comment by [email protected] on 19 Feb 2011 at 11:20

from django-ajax-selects.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 26, 2024

Original comment by [email protected] on 23 Jul 2011 at 7:44

  • Changed state: Done

from django-ajax-selects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.