开发者

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

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.

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 he开发者_JS百科lp would be appreciated.


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

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:

# 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜