0

The basic problem is that I can't use the key-value-argument

field_variable_name__isnull=True/False

or

field_variable_name=None

when the field_variable_name contains spaces or hyphens (e.g. 'This is my field variable name', or 'This-is-another-one').

I looked up how to filter a models.object-list with 'varname__isnull=True', but didn't find a solution for the case that a name containing a space-character was given:

In my views.py I have the following function:

def currenttodos(request):
    todos = Todo.objects.filter(user=request.user, time_completed__isnull=True)
    return render(request, 'todo/currenttodos.html', {'todos': todos})

The model Todo in the models.py - file comprises the following field:

time_completed = models.DateTimeField(null=True, blank=True)

This way it works, but actually I wanted to call the time_completed - field differently, for example "Completion time" using the name-parameter:

time_completed = models.DateTimeField(null=True, blank=True, name="Completion time")

Now, the problem is that I can't use the __isnull - parameter since the name contains a space. All of the following examples don't work:

todos = Todo.objects.filter(user=request.user, Completion time__isnull=True)
todos = Todo.objects.filter(user=request.user, "Completion time"__isnull=True)
todos = Todo.objects.filter(user=request.user, "Completion time"=None)
etc.

How can I make this work for names containing spaces or hyphens?

Andreas L.
  • 3,239
  • 5
  • 26
  • 65
  • what is the purpose of using "Completion time" as name, there is always label that you can use – soField Jan 27 '21 at 13:17
  • Where would I employ the `label`-parameter? Django model fields don't comprise this one (just got an error `got an unexpected keyword argument 'label'`). – Andreas L. Jan 27 '21 at 13:23

2 Answers2

0

You could delete the name argument. It is not necessary. django will create a field with label of the attribute time_completed but you just filter like this

todos = Todo.objects.filter(user=request.user, time_completed__isnull=True)

Dad
  • 377
  • 2
  • 8
  • I'm afraid my question was not understood. I already know that this works, thank you. My concern is if it's possible at all to change the name or label to something else containing spaces and hyphens? – Andreas L. Jan 27 '21 at 14:02
  • You want create a field in one table by django and set a custom name for it? is it right? – Dad Jan 27 '21 at 14:05
  • I want to create a django model field with a custom name which is different to the variable name, as stated in my question. Then, I want to check if it's null, but as soon as the custom name contains spaces or hyphens, I cannot carry out this check (see question). It seems like just avoiding to name it that way is a workaround, true, but this is not very elegant just avoiding the problem ;) – Andreas L. Jan 27 '21 at 14:07
  • See the beginning of my post: "The basic problem is that I can't use the key-value-argument `field_variable_name__isnull=True/False` or `field_variable_name=None` when the `field_variable_name` contains spaces or hyphens (e.g. 'This is my field variable name', or 'This-is-another-one')." – Andreas L. Jan 27 '21 at 18:32
0

Here is a link to a similar post: Filtering for empty or NULL names in a queryset

Basically, this user wanted to be able to exclude instances that were null or blank. As you can see there are several options to do this. My preferred approach is to chain together the filter criteria as to me it is more straightforward and easy to understand however you have some options.

brandonris1
  • 455
  • 3
  • 9
  • Thanks for sharing. However, I've checked all the answers and unfortunately none of them answers my question. – Andreas L. Jan 27 '21 at 18:10
  • Care to clarify a bit more? From what I can tell, you are trying to get all instances where a field == '' or field == ' '. If thats the case, then something like YourModel.objects.filter(Q(name__exact='') | Q(name__exact=' ')) should work. – brandonris1 Jan 27 '21 at 18:19