Сбой ограничения FOREIGN KEY - Django, SQLite3 - PullRequest
0 голосов
/ 13 апреля 2020

Мое приложение позволяет Tenant профилям подавать заявки на Property просмотры, которые перечислены в Landlord профилях. Я не могу связать две таблицы вместе, я не могу получить ошибку FOREIGN KEY constraint failed.

views.py

from django.shortcuts import render, redirect
from projects.models import Properties, Property_Applications, Property_Reviews
from users.models import Landlord_Profile, Tenant_Profile
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import CreatingListingForm, ListingApplicationForm

def property_apply(request, pk):
    project = Properties.objects.get(pk=pk)
    applyButton = ListingApplicationForm(request.POST)
    profile = request.user.tenant_profile
    if request.method == "POST":
        link = applyButton.save(commit=False)
        link.tenant_apply = profile
        link.listing=project
        link.save()
        messages.success(request, f'You have applied!')
        return redirect('/tenantportal/')
    else:
        link = applyButton
    context = {'project': project, 'applyButton': link}
    return render(request, 'application_submit.html', context)

models.py - проекты

from django.db import models
from PIL import Image
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.conf import settings
from users.models import Landlord_Profile, Tenant_Profile


class Property_Applications(models.Model):
    tenant_apply = models.ForeignKey(Tenant_Profile, on_delete=models.CASCADE, default=1)
    property_owner = models.ForeignKey(Landlord_Profile, on_delete=models.CASCADE,default=1)
    listing = models.ForeignKey('Properties', on_delete=models.CASCADE)
    app_description = models.TextField(default='app description')
    created = models.TextField(default=1)

class Properties(models.Model):
    User = settings.AUTH_USER_MODEL
    landlord = models.ForeignKey(User, on_delete=models.CASCADE)
    address = models.CharField(max_length=100)
    rentPrice = models.FloatField()
    description = models.TextField()
    bedRooms = models.IntegerField()
    bathRoom = models.IntegerField()
    tenantCondtions = models.CharField(max_length=100)
    image = models.ImageField(upload_to='house_preview/')

models.py - пользователи

from django.db import models
from django.contrib.auth.models import User
from PIL import Image

usertypechoices = [(True,'Landlord'),(False,'Tenant')]

class Tenant_Profile(models.Model):
    tenant = models.OneToOneField(User, on_delete=models.CASCADE, unique = True)
    #image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    is_landlord = models.BooleanField(choices=usertypechoices,default=False)
    salary = models.FloatField(null=True)
    savings = models.FloatField(null=True)
    is_hap = models.NullBooleanField(default=False)
...