How can I add a record to a table defined in a django model containing a content_object field using raw sql? -
i got model defined this:
class logentry(models.model): text = models.charfield(max_length = 20) content_type = models.foreignkey(contenttype, null=true, blank=true) object_id = models.positiveintegerfield(null=true, blank=true) content_object = generic.genericforeignkey('content_type', 'object_id') django.contrib.auth.models import user usr = user.objects.all()[0] new_record = logentry.objects.create(text="foobar", content_object=usr)
but how create same record using plain sql? problem of course how determine how insert values fields content_type , content_object.
from django.db import connection, transaction cursor = connection.cursor() cursor.execute(""" insert appname_log_entry set (text, content_type, object_id) calues(%s, %s, %s)""", \ ['foobar', <how content type?>, modelinstance.id])
do have set content_objcet? if how?
i don't understand why want this.
in case, content_type
field standard foreign key. can id same way other fk id, via model_instance.content_type_id
.
Comments
Post a Comment