abolfazlmalekahmadi
abolfazlmalekahmadi

Reputation: 278

need help for change attribute of models on view

i need to change attribute of my models on views but this my code dosent work view:

class TaskRequest(APIView):
    permission_classes = [IsBenefactor,]
    def get(self ,request, task_id):
        obj = Task.objects.get(id=task_id)
        if not obj:
            raise Http404("not found")
        if obj.state==obj.TaskStatus.PENDING:
            data={
                'detail': 'This task is not pending.'
            }
            return Response(data , status=status.HTTP_404_NOT_FOUND)
        else:
            obj.assign_to_benefactor(self , obj.assigned_benefactor)
            obj.save()

            data={
                'detail': 'Request sent.'
            }
            return Response(data , status=status.HTTP_200_OK)

my code of view get object If not available return 404 error if available and state equal to PENDING return some data otherwise change state to Waiting and assigned to benefactor user but this my code doesnt work what can i do ? and my models :

class Task(models.Model):
    class TaskStatus(models.TextChoices):
        PENDING = 'P', 'Pending'
        WAITING = 'W', 'Waiting'
        ASSIGNED = 'A', 'Assigned'
        DONE = 'D', 'Done'

    title = models.CharField(max_length=60)
    state = models.CharField(
        max_length=1,
        default=TaskStatus.PENDING,
        choices=TaskStatus.choices,
    )
    charity = models.ForeignKey(Charity, on_delete=models.CASCADE)
    description = models.TextField(blank=True)
    assigned_benefactor = models.ForeignKey(
        Benefactor,
        on_delete=models.SET_NULL,
        null=True,
    )
    date = models.DateField(null=True, blank=True)
    age_limit_from = models.IntegerField(null=True, blank=True)
    age_limit_to = models.IntegerField(null=True, blank=True)
    gender_limit = models.CharField(
        max_length=2,
        choices=User.Gender.choices,
        default=User.Gender.UNSET,
    )
    def assign_to_benefactor(self, benefactor):
        self.state = Task.TaskStatus.WAITING
        self.assigned_benefactor = benefactor
        self.save()

Upvotes: 0

Views: 95

Answers (2)

Daniel Monjezi
Daniel Monjezi

Reputation: 11

task.assign_to_benefactor(request.user.benefactor)

Upvotes: 1

Niel Godfrey P. Ponciano
Niel Godfrey P. Ponciano

Reputation: 10709

Try changing this lines of code:

obj.assign_to_benefactor(self , obj.assigned_benefactor)
obj.save()

To:

obj.assign_to_benefactor(obj.assigned_benefactor)
  1. You don't need to explicitly pass self because it is automatically added as documented. Also, take note that self within Task should be a Task instance/object, so whatever you are trying to do with obj.assign_to_benefactor(self,... within TaskRequest is incorrect because the self there is a TaskRequest instance/object, not a Task instance object.
  2. Calling obj.save() is already redundant because you are already saving the instance from within Task.assign_to_benefactor
  3. For your consideration, I'm not sure if you have other cases that calls Task.assign_to_benefactor but take note that with the pasted code in the question, you are just assigning the same assigned_benefactor to it.

Upvotes: 1

Related Questions