[Answer]-Confuse for using get queryset and using pass to ignore an exception and proceed in my django view

1πŸ‘

βœ…

Yes, for for you models and method signature you use get right. To simplify things you can get rid of delete()/add() methods by direct assign document_list to M2M relation.

def save_draft(draft, document_list):
    try:
        draft = Draft.objects.get(package=draft.package)
    except Draft.DoesNotExist:
        pass
    except Draft.MultipleObjectsReturned:
        raise CustomException
    draft.draft_document_list = document_list
    draft.save()

EDIT: If there can be only one draft per package then why you use ForeignKey(Package)? With OneToOne relation your code will be much simpler:

def save_draft(draft, document_list):
    draft.draft_document_list = document_list
    draft.save()
πŸ‘€catavaran

Leave a comment