dlyxzen
dlyxzen

Reputation: 243

Include Queryset Key in String Format

I am trying to run a system command with the value of a field from a queryset but not having much luck. And it is returning 'Jobs' object is not subscriptable'. Please see below for relevant information.

Models.py

class Jobs(models.Model):
    user_id = models.CharField(max_length=100)
    template = models.CharField(max_length=100)
    change_ref = models.CharField(max_length=100)
    summary = models.CharField(max_length=100)
    category = models.CharField(max_length=100)

Views

def delete_job(request, job_id):
    record = Jobs.objects.get(pk=int(job_id))
    os.system('mkdir /home/username/' + record['summary'])
    return HttpResponseRedirect("/configen")

I am passing the job_id in through the URL which seems to work fine ( I can delete the record no problem). I was under the impression the 'get' would simply get one record, which I could then reference as a dictionary?

I'm sure there is a simple solution, it doesn't seem to work with string formatting either (using %s or .format()) methods.

Thank you in advance

Upvotes: 0

Views: 65

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599480

You're correct that get does get one record, but wrong that you can reference it as a dictionary. It's a model instance, so you use the normal dot notation: record.summary.

Upvotes: 1

Related Questions