我对 django 中的内容类型感到困惑。首先让我展示我的代码:
模型.py
class Comment(models.Model):
owner = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
posted_at = models.DateTimeField(auto_now_add=True)
content = models.TextField(blank=True, null=True)
contentmarkdown = models.TextField(help_text='Use Markdown syntax.')
content_type = models.ForeignKey(ContentType, limit_choices_to=models.Q(
app_label='post', model='post') | models.Q(app_label='comment',
model='comment'))
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def save(self, *args, **kwargs):
import markdown
self.content = markdown.markdown(self.contentmarkdown)
super(Comment, self).save(*args, **kwargs)
我的意见.py
def create(request):
if request.method == 'POST':
print 'POST data: ', request.POST
form = CommentForm(request.POST)
#app_label, model = request.POST.get('model').split('.')
if form.is_valid():
comment = Comment()
content_type = ContentType.objects.get(app_label="comment", model="comment")
object_id = ?
comment = Comment.objects.create(
content_type = content_type,
object_id = object_id,
contentmarkdown = request.POST.get('contentmarkdown'),
owner= request.POST.get('owner'),
email = request.POST.get('email')
)
return HttpResponseRedirect("/")
网址.py
from django.conf.urls import patterns
urlpatterns = patterns('',
(r'^create/$', 'comment.views.create'),
html
{% load i18n %}
<div class="comment">
<form action="{% url "comment.views.create" %}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }}
{{ field }}<p>
{% endfor %}
<input type="submit" value="{% trans "Submit" %}">
</form>
</div>
表格.py
from django import forms
from comment.models import Comment
from django.forms import ModelForm
class CommentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['owner'].label = 'Name'
self.fields['contentmarkdown'].label = ''
class Meta:
model = Comment
exclude = ['content', 'content_type', 'object_id' ]
现在我的问题是:我收到了这个错误:
object_id may not be NUL
1-我怎样才能得到object_id?2-我应该写什么 object_id = ?3-如果我写这个 request.POST.get(?) 没有什么像 id 一样的东西请你告诉我我怎么能找出 object_id ?