How to reference self as a model with django-ajax-related-fields

时间:2022-12-19 20:29:29

I'm trying to use django-ajax-related-fields, and the foreignkey field I'm trying to create a relation to is the model itself. I've tried 'self', Node, and even self (no quotes), with the same results.

我正在尝试使用与django-ajax相关的字段,而我正在尝试创建关系的foreignkey字段是模型本身。我试过'self',Node,甚至是self(没有引号),结果相同。

This is approximately how I'm trying to use it:

这大约是我试图使用它的方式:

class Node(models.Model):
    parent = ForeignKeyByLetter('self', field_name = "name")
    name = models.CharField(max_length = 256)

I'm really a bit lost on how to use this in the first place, and the docs aren't terribly helpful.

我真的有点迷失在如何使用它,并且文档不是非常有用。

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


1  

According to the documentation, ForeignKeyByLetter is a form field not a model field, which explains why it's not working for you.

根据文档,ForeignKeyByLetter是一个表单字段而不是模型字段,这解释了为什么它不适合你。

I'm not familiar with django-ajax-related-fields either, but from the looks of it, you'll want to try something along the lines of:

我也不熟悉与django-ajax相关的字段,但从它的外观来看,你会想要尝试以下方面:

# in models.py
class Node(models.Model):
    parent = ForeignKey('Node')
    name = models.CharField(max_length = 256)


# in forms.py
from models import Node
from django.forms import ModelForm
from ajax_filtered_fields.forms import ForeignKeyByLetter

class NodeForm(ModelForm):
    class Meta:
        model = Node
    parent = ForeignKeyByLetter(Node, field_name = "name")

Having not used it before, I may be wrong. Here's hoping this will at least put you on the right track.

以前没用过,我可能错了。这里希望这至少会让你走上正轨。

#1


1  

According to the documentation, ForeignKeyByLetter is a form field not a model field, which explains why it's not working for you.

根据文档,ForeignKeyByLetter是一个表单字段而不是模型字段,这解释了为什么它不适合你。

I'm not familiar with django-ajax-related-fields either, but from the looks of it, you'll want to try something along the lines of:

我也不熟悉与django-ajax相关的字段,但从它的外观来看,你会想要尝试以下方面:

# in models.py
class Node(models.Model):
    parent = ForeignKey('Node')
    name = models.CharField(max_length = 256)


# in forms.py
from models import Node
from django.forms import ModelForm
from ajax_filtered_fields.forms import ForeignKeyByLetter

class NodeForm(ModelForm):
    class Meta:
        model = Node
    parent = ForeignKeyByLetter(Node, field_name = "name")

Having not used it before, I may be wrong. Here's hoping this will at least put you on the right track.

以前没用过,我可能错了。这里希望这至少会让你走上正轨。