# Problems with "database relationships"

**URL:** <https://forum.djangoproject.com/t/problems-with-database-relationships/10591>\
**Category:** Using Django\
**Created:** [November 20, 2021, 2:01pm UTC](https://forum.djangoproject.com/t/problems-with-database-relationships/10591 "2021-11-20T14:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [November 20, 2021, 2:01pm UTC](https://forum.djangoproject.com/t/problems-with-database-relationships/10591/1 "2021-11-20T14:01:53Z")

</div>

hello community,  
i have the following database:

```auto
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:

```auto
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 = enter.id

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

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

```

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 😕

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

Thank you in advance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 21, 2021, 12:45pm UTC](https://forum.djangoproject.com/t/problems-with-database-relationships/10591/2 "2021-11-21T12:45:52Z")

</div>

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.  
Example:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ReneS29](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/renes29/32/6365_2.png) [@ReneS29](https://forum.djangoproject.com/u/ReneS29)\
**Post date:** [November 23, 2021, 4:29pm UTC](https://forum.djangoproject.com/t/problems-with-database-relationships/10591/3 "2021-11-23T16:29:24Z")

</div>

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

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 🙂
