Umang Mistry
Umang Mistry

Reputation: 363

Cannot resolve keyword 'id' into field. Choices are: complete, task_priority, text, user, user_id

I am working on a Todo list which has seperate users with their own list. Initially I had the models.py as:

from django.db import models
from django.contrib.auth.models import User



class Todo(models.Model):
    text = models.CharField(max_length=40, default='Test User')
    complete = models.BooleanField(default = False)
    task_priority = models.CharField(max_length=40, default='high')
    def __str__(self):
        return self.text

But then when im trying to link single user with its own set of todotask(text field) and its priorites and status.

I added one to one field like this:

from django.db import models
from django.contrib.auth.models import User



class Todo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key='user_id') #new
    text = models.CharField(max_length=40, default='Test User')
    complete = models.BooleanField(default = False)
    task_priority = models.CharField(max_length=40, default='high')
    def __str__(self):
        return self.text

It threw an error like this:

FieldError at / Cannot resolve keyword 'id' into field. Choices are: complete, task_priority, text, user, user_id

I didnt completely understand the concept of one to one fields and looking at the tutorials online im confused weather I need to make another table and link it to the Todo table or add another field with some primary key?

Help me understand this concept. Thanks in advance.

EDIT: views.py file:

from django.shortcuts import render, redirect
from .models import Todo
from .form import TodoForm,ContactForm
import datetime 
from django.conf import settings
from django.utils import timezone
from django.views.decorators.http import require_POST
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
import json



def index(request):
    todo_list = Todo.objects.order_by('id')
    form = TodoForm()
    mydate = datetime.datetime.now()

    context = {'todo_list': todo_list, 'form': form, 'mydate':mydate}

    return render(request,'todo/index.html',context)

def login(request):
    return render(request, 'todo/login.html')


@require_POST
def addTodo(request):
    form = TodoForm(request.POST)
    text_input = form.get_cleaned_data['text']
    priority_input = form.get_cleaned_data['task_priority']
    if form.is_valid():
        new_todo = Todo(text = text_input, task_priority = priority_input)
        # new_todo = Todo(text = request.POST['text'])
        new_todo.save()
    return redirect('index')

def completeTodo(request, todo_id):
    todo = Todo.objects.get(pk=todo_id)
    todo.complete = True
    todo.save()

    return redirect('index')

def deleteCompleted(request):
    Todo.objects.filter(complete__exact=True).delete()

    return redirect('index')

def deleteAll(request):
    Todo.objects.all().delete()

    return redirect('index')


def emailView(request):

    todo = Todo.objects.all();
    task_list = []
    status_list = []
    for item in todo:
        stritem=str(item)
        task_list.append(stritem)
        if item.complete == True:
            status_list.append('complete')
        else:
            status_list.append('incomplete')


    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():

            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            #maillist = Todo.objects.all()
            email_list=dict(zip(task_list,status_list))
            strlist = json.dumps(email_list)
            content = message + strlist
            try:
                send_mail('Todo List', content ,settings.EMAIL_HOST_USER,[from_email])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "email.html", {'form': form})

def successView(request):
    return HttpResponse('Success! Thank you for your message.')

New models.py:

from django.db import models
from django.contrib.auth.models import User



class Todo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    text = models.CharField(max_length=40, default="")
    complete = models.BooleanField(default = False)
    task_priority = models.CharField(max_length=40, default='high')
    def __str__(self):
        return self.text

Encountered

IntegrityError at /add NOT NULL constraint failed: todo_todo.user_id

Upvotes: 0

Views: 1870

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

This:

user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key='user_id')

isn't at all doing what you think it is. primary_key expects a boolean, not a field name, and isn't to do with the relationship at all. Because the string 'user_id' is not empty, this is being interpreted as True, and is equivalent to:

user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)

which means that user is now the primary key for the Todo model. This is not what you want.

Remove that primary_key clause.

Upvotes: 1

Related Questions