Comments (12)
Is there any progress on this?
from drf-writable-nested.
Hello @isasiluis28! Thank you for the contribution.
UniqueFieldsMixin
cannot work with unique_together
validator for the moment.
But I think it is possible to support this validator.
from drf-writable-nested.
Is there any progress? Is unique_together validator going to be supported in one of the next releases?
from drf-writable-nested.
@ruscoder
Can you give a hint what's necessary to get this issue fixed?
from drf-writable-nested.
Hi @isasiluis28
Thank you for the contribution.
Can you add a test which reproduces the bug?
from drf-writable-nested.
Sure @ir4y
Suppose we add a new register via POST like this
This is the object we have created
Then if we try to update creating a new object for producto_detalles (without the ID field, this is because we want to delete the other objects), but with same ID of veterinaria that is already there before the update (which by the way is the one with a constraint)
Then it raises an IntegrityError
I think the problem is that is trying to create the new reverse relations without deleting the previous ones, so at that point we still have the previous objects, and when it tries to create the new relations it will raise an IntegrityError beacause of the existance of an object that already satisfies the constraint.
I think that the algorithm has to remove first the relations where the IDs are not found anymore, then create the new reverse relations.
Looking at the code that handles the updates in NestedUpdateMixin I found that first tries to create or update the new relations, then deletes the relations that are no longer needed, doesn't it have to be the other way around? meaning first delete the relations, then create the new ones?
from drf-writable-nested.
I mean fork drf-writable-nested
and add the test which will fail. TDD style.
Reproduce your issue in code.
from drf-writable-nested.
@ruscoder , any advice on how to implement this? I'd give it a go.
from drf-writable-nested.
UniqueFieldsMixin does not work with unique_together for me
I have to send an empty list that deletes the items first then send the new items
I guess the way it works by default, it deletes the previous nested items and creates a new ones, we should await and confirm the items are deleted before sending in new items otherwise it'll raise an IntegrityError
from drf-writable-nested.
The only way I can get this to work is by passing an ID or making one of my unique fields a primary key.
from drf-writable-nested.
I hit a wall on this as well.
from drf-writable-nested.
Hi. Unfortunately, I haven't used drf-writable-nested for more than 6 years and I can't give a hint on how to implement it properly without diving into the implementation, but according to the discussion above there's a workaround suggested in #50 that has one limitation, but it might be not relevant for someone who affected by this issue.
So, I can recommend something like
class CustomWritableNestedModelSerializer(WritableNestedModelSerializer):
def update(self, instance, validated_data):
relations, reverse_relations = self._extract_relations(validated_data)
# Create or update direct relations (foreign key, one-to-one)
self.update_or_create_direct_relations(
validated_data,
relations,
)
# Update instance
instance = super(NestedUpdateMixin, self).update(
instance,
validated_data,
)
self.delete_reverse_relations_if_need(instance, reverse_relations)
self.update_or_create_reverse_relations(instance, reverse_relations)
instance.refresh_from_db()
return instance
that is actually copy-paste of original update with changed order of invocation of delete_reverse_relations_if_need and update_or_create_reverse_relations (according to the suggestions from #50).
In general, if someone would like to solve a conflict between #50 and #36 it might be merged into the main branch.
from drf-writable-nested.
Related Issues (20)
- Missing type annotations for PEP 561 typed package
- "depth" property does not work? HOT 1
- You cannot call `.save()` after accessing `serializer.data`.If you need to access data before committing to the database then inspect 'serializer.validated_data' instead. HOT 2
- UniqueFieldMixin does not work for djangorestframwork of versions >= 3.11 HOT 1
- ValueError when trying to set a related id field to the wrong value HOT 1
- Parent instance is inaccesible in child validate data for reverse HOT 1
- Update nested related OneToOneField without providing its pk
- Delete reverse relations before update or create in NestedUpdateMixin HOT 4
- Migrate to new CI platform HOT 1
- Take into account the unique_together Models Meta field in UniqueFieldsMixin HOT 1
- The `.create()` method does not support writable nested fields by default
- Validation of child runs twice. Is this deliberate as it seems a bit inefficient to run the same code twice. HOT 5
- Error when trying to update reverse many-to-many relation
- Creating multiple objects with drf-writable-nested WritableNestedModelSerializer not creating the nested relation objects HOT 2
- how to use with self related field(manytomany)
- [Security] Changing related objects HOT 3
- Setting null when sending data as form data in one-to-one relationships
- The `.create()` method does not support writable nested fields by default. HOT 1
- Problem of pass through values to nested serializers from the call to the base serializer's save method. HOT 3
- Reverse Foreign Key is not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drf-writable-nested.