Skip to content
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

Related models not added when using full=True #571

Open
ukch opened this issue Jul 17, 2012 · 0 comments
Open

Related models not added when using full=True #571

ukch opened this issue Jul 17, 2012 · 0 comments

Comments

@ukch
Copy link

ukch commented Jul 17, 2012

From #564. Similar to #307.

Consider an example app with two models, Continent and Country, where a continent may have many countries, and a country must belong to one continent. The resources are set up as follows:

class ContinentResource(ModelResource):
    countries = fields.ToManyField("pietest.api.CountryResource", "countries", full=True, related_name="continent")

    class Meta:
        queryset = Continent.objects.all()
        include_resource_uri = False
        excludes = ["id"]
        authorization = Authorization()  # for testing only


class CountryResource(ModelResource):

    class Meta:
        queryset = Country.objects.all()
        include_resource_uri = False
        excludes = ["id"]

The use of full=True should mean that I am able to post JSON such as the following, in order to create one continent and one country beneath it:

{
   "name": "Europe", 
   "countries": [
        {
            "name": "France"
        }
    ]
}

However, on posting this data the continent is successfully created, but then the process fails when creating the country, and the following traeback is returned:

Traceback (most recent call last):

  File "lib/tastypie/resources.py", line 192, in wrapper
    response = callback(request, *args, **kwargs)

  File "lib/tastypie/resources.py", line 397, in dispatch_list
    return self.dispatch('list', request, **kwargs)

  File "lib/tastypie/resources.py", line 427, in dispatch
    response = method(request, **kwargs)

  File "lib/tastypie/resources.py", line 1165, in post_list
    updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs))

  File "lib/tastypie/resources.py", line 1784, in obj_create
    self.save_m2m(m2m_bundle)

  File "lib/tastypie/resources.py", line 1951, in save_m2m
    related_bundle.obj.save()

  File "lib/django/db/models/base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)

  File "lib/django/db/models/base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)

  File "lib/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)

  File "lib/django/db/models/query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "lib/django/db/models/sql/compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)

  File "lib/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)

  File "lib/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)

  File "lib/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)

IntegrityError: null value in column "continent_id" violates not-null constraint

This issue can be worked around by specifying the backwards relation (à la this Stackoverflow post), but this then causes the related issue (or non-issue) #564.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant