Skip to content

Instantly share code, notes, and snippets.

@guyjacks
Last active May 2, 2017 19:27
Show Gist options
  • Save guyjacks/cecd1dfad1323a4be8a27099ea3e90c0 to your computer and use it in GitHub Desktop.
Save guyjacks/cecd1dfad1323a4be8a27099ea3e90c0 to your computer and use it in GitHub Desktop.
# viewset
class DealViewSet(viewsets.ModelViewSet):
serializer_class = serializers.DealSerializer
lookup_field = 'client_generated_id'
def get_queryset(self):
case_id = self.kwargs['case_client_generated_id']
return Deal.objects.filter(case__client_generated_id=case_id)
# STACK OVERFLOW - Problem #2
# If I add a perform_create() method then self.request.data will contain the serialized notes, BUT...
# they will be serialized as a long string
def perform_create(self, serializer):
print(self.request.data)
# STACK OVERFLOW - print() output
# You can see that notes is a list of one long string instead of a list of dicts
"""<QueryDict: {'created': ['1493605340'], 'notes': ["{'connecting_word': None, 'parent_note_id': None, 'note_id': '6ea363c2-6766-4ec5-a042-29244aebfc98'}", "{'connecting_word': 'if', 'parent_note_id': '6ea363c2-6766-4ec5-a042-29244aebfc98', 'note_id': '9c7f6c03-c6ce-427c-9830-032c7d35810f'}", "{'connecting_word': 'unless', 'parent_note_id': '9c7f6c03-c6ce-427c-9830-032c7d35810f', 'note_id': 'b799347d-1b6e-4d35-a7f8-8115967e89a9'}"], 'case_id': ['fdb1a45c-3cc7-4336-ae38-0feb8c4a3f53'], 'accepted': ['False'], 'id': ['ff3ec5de-a4ea-455d-b171-ba9d9c3b3ab3']}>"""
notes = self.request.data['notes']
serializer.save(notes=notes)
# serializers
class DealNoteSerializer(serializers.ModelSerializer):
note_id = serializers.SlugRelatedField(slug_field='client_generated_id', source='note', queryset=Note.objects.all())
parent_note_id = serializers.SlugRelatedField(slug_field='client_generated_id', source='parent', queryset=Note.objects.all())
class Meta:
model = DealNote
fields = ('note_id', 'connecting_word', 'parent_note_id')
class DealSerializer(CaseChildSerializer):
id = serializers.UUIDField(source='client_generated_id')
case_id = serializers.SlugRelatedField(slug_field='client_generated_id', source='case', queryset=Case.objects.all())
# STACK OVERFLOW: passing read_only=True|False has no impact on this issue.
notes = DealNoteSerializer(many=True, source='dealnote_set')
created = UnixEpochDateTimeField(required=True)
class Meta:
model = Deal
fields = ('id', 'case_id', 'accepted', 'notes', 'created')
def create(self, validated_data):
# STACK OVERFLOW Problem #1
# If I do NOT implement a perform_create() method on the above serializer, then
# validated_data does not contain "notes"... Why?
# The docs (http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers)
# say what I have should worked.
print(validated_data)
"""
print output - the dealnote_set is empty
data {'case': <Case: maiores>, 'accepted': False, 'dealnote_set': [], 'client_generated_id': UUID('e6c52f0e-6cf4-4fce-bff9-a441252eb3b6'), 'created': datetime.datetime(1970, 1, 18, 6, 53, 25, 340000, tzinfo=<django.utils.timezone.LocalTimezone object at 0x10cdf9dd8>)}
"""
# leaving out the rest ...
# Models
class Deal(models.Model):
client_generated_id = models.UUIDField(editable=True, unique=True)
case = models.ForeignKey(Case)
notes = models.ManyToManyField(Note, through='DealNote', through_fields=('deal', 'note'))
accepted = models.BooleanField()
created = models.DateTimeField()
class Meta:
verbose_name = _('deal')
verbose_name_plural = _('deals')
def __str__(self):
return str(self.id)
class DealNote(models.Model):
AND = 'and'
OR = 'or'
IF = 'if'
UNLESS = 'unless'
CONNECTING_WORDS = (
(AND, 'and'),
(OR, 'or'),
(IF, 'if'),
(UNLESS, 'unless'),
)
deal = models.ForeignKey(Deal)
note = models.ForeignKey(Note)
connecting_word = models.CharField(max_length=40, choices=CONNECTING_WORDS, blank=True, null=True, default=None)
parent = models.ForeignKey(Note, related_name='parent', blank=True, null=True, default=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment