django の contenttype と混同しています。まず、私のコードを表示させてください:
models.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)
私のviews.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("/")
urls.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 のようなものはありません。