Re: Create an object before calling the save method
Hi, Thank you for your answer. I will explain what I want to do. Let's say that we have Category and Article classes in our model, each one has a title. To make this title reusable, I created another application that will manage fields, I created the class Title and I added it as foreignkey to Category and Article forms. I switched the select box to an input field using raw_id_fields. Now, when I create a category or an article, I have to select or write a title, when this title exists, it works perfectly but when it doesn't exist I want to create it before creating the category so it can use it. I tried to do that in the save method, in the pre_save signal and in the clean method but I always get the error "Select a valid choice. That choice is not one of the available choices." I'm using a hard coded solution to create the title now, I want just to see if it will work, these are the lines that I inserted in the different methods to create the title before creating the category : t = Title(title = "MyTitle") t.save() I tried to create a Category with MyTitle as title but I get the same error, when I try to create another one using an existing title, it works and the title "MyTitle" is created. That's mean that the creation of the object happens after the form verification. What I want is just doing this before. The title object should be created before the verification. Thank you very much for your help -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Create an object before calling the save method
On Nov 27, 2010, at 2:56 PM, bnabilos wrote: > What I want to do is creating a title based on what I write in the > ForeignKey field before creating the category so it can be used > immediately. You can do just that. Just accept the title in a CharField, without a choices= option, and then create the Title based on that, then creating the category to refer to it. The code would look something like this: t = Title(title=form.cleaned_data['title']) t.save() # This creates the primary key for the new Title object c = Category(title=t) c.save() You can add appropriate checks if the title and category already exist, to handle those the way you wan tto. -- -- Christophe Pettus x...@thebuild.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Create an object before calling the save method
Hello I want to create an object in Django before calling the save method. This object will be created from a ForeignKey Value, I've changed the foreignkey field to look like an input field in order to write a value instead of selecting it. I have 2 classes in 2 different model files class Category(models.Model): title = models.ForeignKey(Title, verbose_name="Title") and class Title(models.Model): title = models.CharField("Title", primary_key=True, max_length=200) When I create a category, I have to pick or write a title that already exists in the database and when I try to create a category with a new title I get this error : Select a valid choice. That choice is not one of the available choices. What I want to do is creating a title based on what I write in the ForeignKey field before creating the category so it can be used immediately. I tried to redefine the save method to save the title object before saving the category but it didn't work. Any help will be really appreciated. Thank you -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.