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

ModelViews don't correctly validate Resources whose models have unique fields. #35

Merged
merged 1 commit into from
Jul 1, 2011

Conversation

fzunino
Copy link
Contributor

@fzunino fzunino commented Jul 1, 2011

With this patch ModelViews can have Resources whose models have unique fields.

ReadModelMixin and UpdateModelMixin store model instance as a property. This allows ModelResource to bind the ModelForm using the model instance making the form validate the input data against the model instance and not a brand new instance. When the latter happened and the model used unique fields, the form validation failed whenever a PUT was maintaining the previous value of the unique field.

Example to reproduce:

class Movie(models.Model):
    name = models.CharField(max_length=200)
    imdb_id = models.CharField(max_length=32, blank=True, unique=True, null=True)

class MovieResource(ModelResource):
    model = Movie
    fields = ('name', 'imdb_id', 'url')
    ordering = ('name',)

urlpatterns = patterns('',
    url(r'^movies/$', ListOrCreateModelView.as_view(resource=MovieResource), name='movies'),
    url(r'^movies/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=MovieResource)),
)

$ curl -X POST --data 'name=movie&imdb_id=1' http://localhost:8000/movies  
201 Created

$ curl -X PUT --data 'name=movie&imdb_id=1' http://localhost:8000/movies/1  
400 Bad Request

ReadModelMixin and UpdateModelMixin store model instance as a property. This allows ModelResource to bind the ModelForm using the model instance making the form validate the input data against the model instance and not a brand new instance. When the latter happened and the model used unique fields, the form validation failed whenever a PUT was maintaining the previuos value of the unique field.
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

Successfully merging this pull request may close these issues.

2 participants