Django: Querying read-only view with no primary key

前端 未结 4 1133
广开言路
广开言路 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 14:51

    I have this issue all the time. I have a view that I can't or don't want to change, but I want to have a page to display composite information (maybe in the admin section). I just override the save and raise a NotImplementedError:

        def save(self, **kwargs):
            raise NotImplementedError()
    

    (although this is probably not needed in most cases, but it makes me feel a bit better)

    I also set managed to False in the Meta class.

        class Meta:
           managed = False
    

    Then I just pick any field and tag it as the primary key. It doesn't matter if it's really unique with you are just doing filters for displaying information on a page, etc.

    Seems to work fine for me. Please commment if there are any problems with this technique that I'm overlooking.

提交回复
热议问题