正确使用Django相关字段

时间:2016-11-27 06:31:17

标签: sql django django-models django-1.10 python-3.6

Django的模型的一个方面,我根本不理解的是相关领域,并使他们的用法正确。我认为这反映了对SQL和数据库的一般认识不足 - 所以我怀疑这对许多django用户来说是一个问题。

在我目前的代码中,我正在构建一个旅游应用程序,因此我将住宿作为模型。然后我有路线。这一天的路线最终成为一堆关系领域,但让我感到困惑的是我需要一个入住和最终住宿的事实 - 两个独立的关系。

对我来说,从逻辑上讲,我应该能够将OneToOneField - start_accom放到住宿区,然后将end_accom放到住宿区。但这样做会引发错误。

代码:

class Route(models.Model):                                                                                                  
    start_accom = models.OneToOneField(                                                                                     
        Accommodation,                                                                                                      
        on_delete=models.CASCADE,                                                                                           
        primary_key=True,                                                                                                   
    )                                                                                                                       
    end_accom = models.OneToOneField(                                                                                                                                                                             
        Accommodation,                                                                                                      
        on_delete=models.CASCADE,                                                                                           
    )                                                                                                                       
    mode_of_travel = models.CharField(max_length=50)                                                                        
    description = models.TextField()  

运行django runserver时的错误消息:

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x103ecdd90>
Traceback (most recent call last):
  File "/Users/vernonswanepoel/.virtualenvs/tour/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/vernonswanepoel/.virtualenvs/tour/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "/Users/vernonswanepoel/.virtualenvs/tour/lib/python3.6/site-packages/django/core/management/base.py", line 431, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
company.Route.end_accom: (fields.E304) Reverse accessor for 'Route.end_accom' clashes with reverse accessor for 'Route.start_accom'.
        HINT: Add or change a related_name argument to the definition for 'Route.end_accom' or 'Route.start_accom'.
company.Route.end_accom: (fields.E305) Reverse query name for 'Route.end_accom' clashes with reverse query name for 'Route.start_accom'.
        HINT: Add or change a related_name argument to the definition for 'Route.end_accom' or 'Route.start_accom'.
company.Route.start_accom: (fields.E304) Reverse accessor for 'Route.start_accom' clashes with reverse accessor for 'Route.end_accom'.
        HINT: Add or change a related_name argument to the definition for 'Route.start_accom' or 'Route.end_accom'.
company.Route.start_accom: (fields.E305) Reverse query name for 'Route.start_accom' clashes with reverse query name for 'Route.end_accom'.
        HINT: Add or change a related_name argument to the definition for 'Route.start_accom' or 'Route.end_accom'.

System check identified 4 issues (0 silenced).

1 个答案:

答案 0 :(得分:1)

正如提到的那个问题是因为相反的关系。您需要add the related_name attribute

class Route(models.Model):                                                                                                  
    start_accom = models.OneToOneField(                                                                                     
        Accommodation,                                                                                                      
        on_delete=models.CASCADE,                                                                                           
        primary_key=True, 
        related_name="accommodation_start"                                                                                                  
    )                                                                                                                       
    end_accom = models.OneToOneField(                                                                                                                                                                             
        Accommodation,                                                                                                      
        on_delete=models.CASCADE,                                                                                           
        related_name="accommodation_end"                                                                                                  
    )                                                                                                                       
    mode_of_travel = models.CharField(max_length=50)                                                                        
    description = models.TextField()