AlexandruC
AlexandruC

Reputation: 3637

Django: manipulate model in view

I want to access, update or create a new entry in a database through a django model, from within a viewset.

class UploadView(View):

    def post(self, request):
        file = request.FILES['file']
        data = request.POST['myObj']
        profile_id = request.POST['profile_id']
        profile = ProfileModel.objects.all().filter(pk=profile_id)
        print (profile.id)
        print(profile.details)
        return HttpResponse('got post')

and model

class ProfileModel(models.Model):
    '''
    '''
    id = models.AutoField(primary_key=True)
    details = models.CharField(max_length=256)     
    file = models.BinaryField()
    class Meta:
        db_table = 'Profile'

When it reaches the print statement it throws an exception.

Upvotes: 0

Views: 339

Answers (1)

dm03514
dm03514

Reputation: 55962

profile is a queryset not an individual profile, so it shouldn't have an id field

If you're supposed to have an individual profile you could use the get method

profile = ProfileModel.objects.get(pk=profile_id)

get can throw a DoesNotExist if there is no matchign ProfileModel

Upvotes: 2

Related Questions