在djongo和django中使用ArrayReferenceField的问题

时间:2018-12-27 11:25:48

标签: python django django-models djongo

我正在尝试使用djongo与mongodb通信使用django创建应用程序。我有以下模型:

from djongo import models
from django import forms

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        abstract = True

class BlogForm(forms.ModelForm):
    class Meta:
        model = Blog
        fields = (
            '__all__'
        )

class MetaData(models.Model):
    # pub_date = models.DateField()
    # mod_date = models.DateField()
    n_pingbacks = models.CharField(max_length=255)
    rating = models.CharField(max_length=255)

    class Meta:
        abstract = True

class MetaDataForm(forms.ModelForm):
    class Meta:
        model = MetaData
        fields = (
            '__all__'
        )

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    def __str__(self):
        return self.name

class AuthorForm(forms.ModelForm):
    class Meta:
        model = Author
        fields = (
            'name', 'email'
        )

class Entry(models.Model):
    blog = models.EmbeddedModelField(
        model_container=Blog,
        model_form_class=BlogForm
    )
    meta_data = models.EmbeddedModelField(
        model_container=MetaData,
        model_form_class=MetaDataForm
    )

    headline = models.CharField(max_length=255)
    body_text = models.TextField()

    authors = models.ArrayReferenceField(
        to=Author,
        on_delete=models.CASCADE,
    )
    n_comments = models.CharField(max_length=255)

    def __str__(self):
        return self.headline

但是当我使用django admin创建一个新条目时,这是我得到的错误:

  / admin / app / entry / add / int()参数中的

TypeError必须是字符串,   类似字节的对象或数字,而不是“设置”请求方法:POST请求   URL:http://localhost:8000/admin/app/entry/add/ Django版本:2.1.3   异常类型:TypeError异常值:int()参数必须为   字符串,类似字节的对象或数字,而不是“设置”   位置:C:\ Users \ scg \ Documents \ Rest_project \ envschool \ lib \ site-packages \ django \ db \ models \ fields__init __。py   在get_prep_value中,第965行Python   可执行文件:C:\ Users \ scg \ Documents \ Rest_project \ envschool \ Scripts \ python.exe   Python版本:3.7.1 Python路径:
  ['C:\ Users \ scg \ Documents \ Rest_project \ djongoproj2',   'C:\ Users \ scg \ Documents \ Rest_project \ envschool \ Scripts \ python37.zip',   'C:\ Users \ scg \ AppData \ Local \ Programs \ Python \ Python37-32 \ DLLs',   'C:\ Users \ scg \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib',   'C:\ Users \ scg \ AppData \ Local \ Programs \ Python \ Python37-32',   'C:\ Users \ scg \ Documents \ Rest_project \ envschool',   'C:\ Users \ scg \ Documents \ Rest_project \ envschool \ lib \ site-packages']

请问如何解决此问题。

我正在使用Django 2.1.3和djongo 1.2.30

1 个答案:

答案 0 :(得分:-1)

我遇到了类似的问题。我通过降级到Django 2.0来解决了这个问题。