Problem with get_or_create

56 views
Skip to first unread message

Murilo Vicentini

unread,
Apr 23, 2012, 9:26:52 AM4/23/12
to django...@googlegroups.com
Hey guys,
I'm having a bit of a problem using the get_or_create with a ManyToManyField in my model.
class Run(models.Model):
        distros = models.ForeignKey('Distro')
        compilers = models.ManyToManyField('Compiler', blank=True, null=True)
        adapters = models.ForeignKey('Adapter')

        Task = models.IntegerField()
        Path = models.CharField(max_length = 300)
        Date_Time = models.DateTimeField()

class RunForm(ModelForm):
        class Meta:
                model = Run

What I'm trying to do is merely trying to save in the database if the entry does not exist already (hence why I'm trying to use get_or_create). Here is how I'm doing:
fields = {
         'Task': task,
         'Path': runpath,
         'Date_Time': datetime(2012, 4, 10, 10, 20, 0),
         'adapters': adapters.id,
         'distros': distros.id,   
}
form = RunForm(fields)
if form.is_valid():
          runs, created = Run.objects.get_or_create(**form.cleaned_data)

In the compilers field I don't want to set any value this time. And what I keep getting the following error: int() argument must be a string or a number, not 'list'. Does anyone know why this is happening? Is it not possible to use the get_or_create with many to many relationship in the model?

akaariai

unread,
Apr 23, 2012, 11:42:19 AM4/23/12
to Django users
On Apr 23, 4:26 pm, Murilo Vicentini <murilofvicent...@gmail.com>
wrote:
Try "runs = form.save()", get_or_create doesn't seem to be the correct
method in this situation.

If you want to skip some fields you can use the form's Meta attributes
to do that.

- Anssi

Denis Darii

unread,
Apr 23, 2012, 11:47:24 AM4/23/12
to django...@googlegroups.com
Murilo, try to debug your "task" variable - must be an int.
Also next time please provide full traceback of your error.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
I'm using Linux because i'm freedom dependent.

Reply all
Reply to author
Forward
0 new messages