我无法添加外键值

时间:2020-07-22 15:01:46

标签: django django-models django-views

我需要将数据添加到数据库中,为此,我正在尝试一些代码。但是我不能添加外键值。该代码引发异常。这是我的views.py,models.py代码和异常。

首先尝试: views.py

def notification(request):
  user = request.user 
  if request.method == 'POST':

   property_id = request.POST['property_id'] 
   owner = request.POST['owner_id']
   property_object = Property.objects.get(id=property_id)
   owner_object = Property.objects.get(owner=owner)
   notification =  user, "have intrested in your property" 
   property_object.notify.add(user)
   notifications = Notifications.objects.create(notification=notification, owner=owner_object 
   ,property=property_object)

   notifications.save()

它引发异常

ValueError: Field 'id' expected a number but got 'hafis'.

第二次尝试 views.py

  def notification(request):
    user = request.user 
    if request.method == 'POST':

       property_id = request.POST['property_id'] 
       owner = request.POST['owner_id']
       property_object = Property.objects.get(id=property_id)
       notification =  user, "have intrested in your property" 
       property_object.notify.add(user)
       notifications = Notifications.objects.create(notification=notification, owner=owner, 
       property=property_id)

       notifications.save()

它引发异常

ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance.

models.py

class Property(models.Model):
   owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
   headline = models.CharField(max_length=255)
   city = models.CharField(max_length=100)
   location = models.CharField(max_length=100)
   facilites = models.TextField()
   rent = models.CharField(max_length=200)
   images = models.FileField(upload_to='media/uploads',null=True)
   email = models.EmailField()
   mobile = models.IntegerField(null=True)
   date = models.DateTimeField(auto_now_add=True)
   notify = models.ManyToManyField(User, default=None, blank=True, related_name='Liked')

def __str__(self):
    return self.headline 


 class Notifications(models.Model):
    owner = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
    property = models.ForeignKey(Property, null=True, blank=True,on_delete=models.CASCADE)
    notification = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

   def __str_(self):
      return self.notification

HTML代码

{% if accomodations %}  
            {% for accomodation in accomodations %}
            <form action="{% url 'notification'  %}" method="post">
                {% csrf_token %}
            <input type="hidden" name="owner_id" value="{{ accomodation.owner }}">
            <button id="request-btn" name="property_id" value="{{ accomodation.id }}" class="btn btn-primary">Contact info:</button>
            </form>

2 个答案:

答案 0 :(得分:0)

ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance.您只是传递了ID,但它要求它是User对象。

def notification(request):
    user = request.user 
    if request.method == 'POST':

       property_id = request.POST['property_id'] 
       owner = User.objects.get(id=request.POST['owner_id'])
       property_object = Property.objects.get(id=property_id)
       notification =  user, "have intrested in your property" 
       property_object.notify.add(user)
       notifications = Notifications.objects.create(notification=notification, owner=owner, 
       property=property_id)

       notifications.save()

答案 1 :(得分:0)

您需要传递用户实例。您可以通过此行代码吸引用户

owner = User.objects.all().get(username=owner)

注意: 每个用户都应使用不同的用户名