Django: Querying read-only view with no primary key

前端 未结 4 1135
广开言路
广开言路 2020-12-28 14:41
class dbview(models.Model):
    # field definitions omitted for brevity
    class Meta:
        db_table = \'read_only_view\'

def main(request):
    result = dbview         


        
4条回答
  •  醉话见心
    2020-12-28 15:05

    When you say 'I have no control over the view I am accessing with Django. MySQL browser shows columns there but no primary key.'

    I assume you mean that this is a legacy table and you are not allowed to add or change columns?

    If so and there really isn't a primary key (even a string or non-int column*) then the table hasn't been set up very well and performance might well stink.

    It doesn't matter to you though. All you need is a column that is guaranteed to be unique for every row. Set that to be 'primary_key = True in your model and Django will be happy.

    • There is one other possibility that would be problemmatic. If there is no column that is guaranteed to be unique then the table might be using composite primary keys. That is - it is specifying that two columns taken together will provide a unique primary key. This is perfectly valid relational modelling but unfortunatly unsupported by Django. In that case you can't do much besides raw SQL unless you can get another column added.

提交回复
热议问题