我有两个模型:
# Default server configuration
#
server {
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
listen 80;
# listen on both hosts
server_name example.com;
index index.php index.html index.htm;
location / {
include cors;
try_files $uri $uri/ /index.php$is_args$args;
}
if ($http_x_forwarded_proto = "http") {
return 301 https://$server_name$request_uri;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php/php7.0-fpm.sock
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Path for static files
root /home/ubuntu/apps/mysite.com/website/;
#Specify a charset
charset utf-8;
# Custom 404 page
error_page 404 /404.html;
# Include the basic h5bp config set
#include h5bp/basic.conf;
ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
}
我想显示与事件相关的每个来源,但这不起作用:
class Incident(models.Model):
iid = models.IntegerField(primary_key=True)
class Source(models.Model):
sid = models.IntegerField(primary_key=True)
incident = models.ForeignKey('Incident', on_delete=models.SET_NULL, null=True)
url = models.TextField(validators=[URLValidator()])
答案 0 :(得分:4)
外键的默认反向名称为<modelname>_set
。所以试试这个:
{% for source in incident.source_set.all %}
您可以使用related_name
选项更改反向名称:
incident = models.ForeignKey('Incident', on_delete=models.SET_NULL, null=True, related_name='sources')
在这种情况下,事件的sources
属性将为您提供源对象:
{% for source in incident.sources.all %}