3

I've a snip of code like following:

models.py

class Notebook(models.Model):
    owner = models.ForeignKey(User, on_delete = models.CASCADE)
    name= models.CharField(max_length=50)

class Note(models.Model):
    create_user = models.ForeignKey(User, on_delete = models.CASCADE)
    text=models.CharField(max_length=500)
    notebook=models.ForeignKey(Notebook, on_delete = models.CASCADE, limit_choices_to = {'owner' : create_user})

But I'm getting an error that limit_users_to cannot be a Foreign Key. I want to users to select only notebooks they have created while writing a note, but now users can select other's notebook while limit_choices_to is not set. And notebook must be ForeignKey. What can i do?

polu_
  • 342
  • 7
  • 17
  • Sounds to me that this restriction should be done at forms level and not at models. Because it depends on request (logged in user). Also `create_user` looks like unnecessary field if `owner` and `create_user` are same. – anupsabraham Oct 21 '17 at 09:56
  • Why would the Note.notebook FK point to a User? and not to a Notebook? In any case, if you have two FK pointing to the same model (here: User) then specify the related_name attribute of the FKs. see: https://stackoverflow.com/questions/583327/django-model-with-2-foreign-keys-from-the-same-table – rollinger Oct 21 '17 at 10:02
  • @anupsabraham Can you give me an example how can I make such restrictions in forms level? – polu_ Oct 21 '17 at 10:09
  • @PalashBauri I don't understand your model now. I was thinking `notebook` is a FK to your `Notebook` model until rollinger's comment. You need to do a little more for us to be able to answer your question. Do you have a form created where user can enter these details? – anupsabraham Oct 21 '17 at 10:14
  • @anupsabraham sorry I fixed the mistake. Now Note.notebook FK points to the correct thing. – polu_ Oct 21 '17 at 10:22
  • 1
    Possible duplicate of [Django: Current User Id for ModelForm Admin](https://stackoverflow.com/questions/28038599/django-current-user-id-for-modelform-admin) – Brown Bear Oct 21 '17 at 10:22
  • @rollinger I fixed the Note.notebook mistake – polu_ Oct 21 '17 at 10:22

1 Answers1

2

You have to do that in the View when creating a Note

form.py

from .models import Note
from django.forms import ModelForm

class NoteForm(ModelForm):
    class Meta:
       model = Note

view.py

from django.views.generic.edit import CreateView
from .form import NoteForm
from .models import Note, Notebook

class NoteCreateView(CreateView):
    model=Note
    form_class=NoteForm

    def get_form(self, form_class=None):
        form = super(NoteCreateView, self).get_form(form_class)
        # Thats the solution:
        form.fields['notebook'].queryset = Notebook.objects.filter(owner=self.request.user)
        return form
Pablo Vergés
  • 319
  • 2
  • 8
rollinger
  • 531
  • 1
  • 4
  • 15
  • What is the wished behavior for notebooks created by the user, which contain no notes? Should these appear on the list or not? – Pablo Vergés Oct 21 '17 at 11:59
  • Have to appear there otherwise you never would be able to add a note to any notebook. This is also out of scope of the question. – rollinger Oct 21 '17 at 12:02
  • It’s not clear from the users question. You’re totally right, that’s the expected behavior. I just wondered if that was what Palash Bauri wants. Else your queryset needs to change. I don’t see how this is out of scope (but that discussion is really out of scope). – Pablo Vergés Oct 21 '17 at 12:20