Я получил модель, определенную так:
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')
from django.contrib.auth.models import User
usr = User.objects.all()[0]
new_record = LogEntry.objects.create(text="foobar", content_object=usr)
Но как мне создать ту же запись, используя обычный SQL? Проблема, конечно, в том, как определить, как вставить значения в поля content_type и content_object.
from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("""
insert into appname_log_entry SET (text, content_type, object_id)
calues(%s, %s, %s)""", \
['foobar', <how to get content type?>, ModelInstance.Id])
Нужно ли устанавливать content_objcet? Если так, то как?