foreign-keys

Users as foreign key in Django

Users as foreign key in Django Question: I have the below in my models.py file: class Film(models.Model): title = models.CharField(max_length=200) director = models.CharField(max_length=200) description = models.CharField(max_length=200) pub_date = models.DateField(‘date published’) class Comment(models.Model): film = models.ForeignKey(Film, on_delete=models.CASCADE) body = models.CharField(max_length=200) When I logged into Django admin I added some films, and then added some comments, selecting …

Total answers: 2

Django 2.0: sqlite IntegrityError: FOREIGN KEY constraint failed

Django 2.0: sqlite IntegrityError: FOREIGN KEY constraint failed Question: I’m working on adding Django 2.0 support to the django-pagetree library. During automated testing, using an sqlite in-memory database, I’m getting a bunch of errors like this: File “/home/nnyby/src/django-pagetree/pagetree/tests/test_models.py”, line 638, in setUp ‘children’: [], File “/home/nnyby/src/django-pagetree/pagetree/models.py”, line 586, in add_child_section_from_dict … File “/home/nnyby/src/django-pagetree/venv/lib/python3.5/site-packages/django/db/backends/base/base.py”, line 239, …

Total answers: 10

django – TypeError how to get instance of a foreignkey from the user instance

django – TypeError how to get instance of a foreignkey from the user instance Question: I am getting the following type error and its due to this instance in view. The model as you can see below where Employee is onetoone relation to User and Company is the foreignkey to the Employee. How would I …

Total answers: 1

Django error. Cannot assign must be an instance

Django error. Cannot assign must be an instance Question: I get the following error when I try to run an insert into one of my tables. Cannot assign “1”: “Team.department_id” must be a “Department” instance Admittedly I’m slightly unsure if I’m using the foreign key concept correctly. The insert I’m trying to run and a …

Total answers: 3

sqlalchemy: create relations but without foreign key constraint in db?

sqlalchemy: create relations but without foreign key constraint in db? Question: Since sqlalchemy.orm.relationship() already implies the relation, and I do not want to create a constraint in db. What should I do? Currently I manually remove these constraints after alembic migrations. Asked By: est || Source Answers: Instead of defining “schema” level ForeignKey constraints create …

Total answers: 1

foreignkey (user) in models

foreignkey (user) in models Question: I read the docs and this post… Django – Foreign Key to User model I followed what it said and I still cannot get it to work. When I try to run the migrations I get this error in the traceback… django.db.utils.ProgrammingError: column "author_id" cannot be cast automatically to type …

Total answers: 4

ForeignKey field related to abstract model in Django

ForeignKey field related to abstract model in Django Question: I have this model: class BaseModel(models.Model): …. class Meta: abstract = True class ModelA(BaseModel): …. class ModelB(BaseModel): …. class MyExtModel(models.Model) myfield = models.ForeignKey(BaseModel) But this is not correct because I have BaseModel like Abstract. Infact I have an error when I try makemigration command. The error …

Total answers: 3

Django Rest Framework – Get related model field in serializer

Django Rest Framework – Get related model field in serializer Question: I’m trying to return a HttpResponse from Django Rest Framework including data from 2 linked models. The models are: class Wine(models.Model): color = models.CharField(max_length=100, blank=True) country = models.CharField(max_length=100, blank=True) region = models.CharField(max_length=100, blank=True) appellation = models.CharField(max_length=100, blank=True) class Bottle(models.Model): wine = models.ForeignKey(Wine, null=False) user …

Total answers: 2

How to use 'User' as foreign key in Django 1.5

How to use 'User' as foreign key in Django 1.5 Question: I have made a custom profile model which looks like this: from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.ForeignKey(‘User’, unique=True) name = models.CharField(max_length=30) occupation = models.CharField(max_length=50) city = models.CharField(max_length=30) province = models.CharField(max_length=50) sex = models.CharField(max_length=1) But when I run …

Total answers: 2

Inserting new records with one-to-many relationship in sqlalchemy

Inserting new records with one-to-many relationship in sqlalchemy Question: I’m following the flask-sqlalchemy tutorial on declaring models regarding one-to-many relationship. The example code is as follows: class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) addresses = db.relationship(‘Address’, backref=’person’, lazy=’dynamic’) class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50)) person_id = db.Column(db.Integer, db.ForeignKey(‘person.id’)) Now I’m …

Total answers: 4