Problems with "database relationships"

hello community,
i have the following database:

class Projects(models.Model):
    project = models.CharField(max_length = 999)
    domain = models.CharField(max_length = 999)

class KeywordMonitoring(models.Model):
    project = models.ForeignKey(to="Projects", on_delete=models.CASCADE)
    keyword = models.CharField(max_length = 999)
    keywordTag = models.CharField(max_length = 999)

and here is my view:

if request.method == "POST":
        _Project = request.POST["project"]
        _Project_ID = 0
        _Keywords = request.POST["keywords"]
        _KeywordTag = request.POST["keywordTag"]

        for enter in Projects.objects.all():
            if enter.project == _Project:
                _Project_ID =

        keywordSet = _Keywords.split(",")
        for keyword in keywordSet:
            newKeyword = keyword.strip()

            KeywordMonitoringModel = KeywordMonitoring(keyword=newKeyword, keywordTag=_KeywordTag, project=_Project_ID)

I get the following error:
ValueError: Cannot assign “1”: “KeywordMonitoring.project” must be a “Projects” instance.

I’ve already browsed the documentation - but unfortunately I don’t know exactly what to look for :confused:

The “” Projects “instance” is the ID - or am I wrong?

Thank you in advance

You didn’t post the full traceback so I’m guessing that error is being thrown here:
KeywordMonitoringModel = KeywordMonitoring(keyword=newKeyword, keywordTag=_KeywordTag, project=_Project_ID)

And the issue is the last parameter: project=_Project_ID

Since you’re assigning the ID directly, and not from a reference to a Projects object, you would refer to the field as the ID field: project_id=_Project_ID

Using the object reference name is appropriate when you have an instance of the object.

a_project = Projects.objects.get(id=_Project_ID)
a_keywork_monitoring = KeywordMonitoring(keyword=newKeyword, keywordTag=_KeywordTag, project=a_project)

Note: I would never do it this way unless I already had a need in the view to use a_project for other purposes within the view. It’s not worth doing the database query just to be able to assign an object reference.

Side note: I suggest you start adopting the Python / Django coding conventions.
e.g. Classes are capitalized, instances are lower-case, underscore separated. Python does not use camel-case. So it would be new_keyword and not newKeyword or keyword_monitoring_model instead of KeywordMonitoringModel.

It may not make a difference to you now, but it’s going to make things easier for other people who are going to read your code (such as the people on this forum who will try to provide assistance as appropriate), and to yourself down the road when you start reading other code or needing to acquire a deeper understanding of how Django works by reading its source code.

Side note #2: From a database design perspective, the common convention for tables are that they are named for the singular of the entities being stored, not the plural. (“Project”, not “Projects”) Django assumes this convention, most visually apparent in the admin.

1 Like

Thank you very much for the detailed answer and the additional advice - I will be happy to use it in the future :slight_smile:

You have already helped me with several problems - I find it really commendable when you give “newbies” a helping hand in such a committed manner :slight_smile: