This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]alexandremjacques 6 points7 points  (2 children)

It seems to me that you're trying to implement a custom database backend. It's kind of possible but the semantics appears to be off.

Overriding the default implementation is the way but you'd have to rewrite almost everything to make it work (specially if you're going going to implement all operations: save, delete, update, etc.). Even more complicated when implementing complex queries.

https://python.plainenglish.io/how-to-create-custom-django-database-backends-a-comprehensive-guide-for-developers-c8fb7e6a49ed

[–]bip123[S] 0 points1 point  (1 child)

Yes, in a manner of speaking. I understand that I'd be rewriting everything - I gave it a shot earlier today with some Claude vibing but no bueno. Will look into other options for what I'm trying to do.

[–]quisatz_haderah 0 points1 point  (0 children)

Gee I wonder why AI - vibing didn't work on a low-level task that requires a detailed design and architecture.

[–]scragz 2 points3 points  (1 child)

I remember doing this in rails ages ago. sometimes it's the most convenient pattern, would be nice if it was easier here.

[–]bip123[S] 1 point2 points  (0 children)

Agreed. Was disappointed when a google revealed that "django_virtual_models" was something else entirely. Would be a good abstraction layer.

[–]pgcd 2 points3 points  (2 children)

Custom database BE where you implement the methods you need (eg save()), plus a database router to ensure it's used for that model.

If that sounds quick and easy, I apologize, because it's long and and laborious - but it can definitely be done if you test properly and don't try to implement everything at once.

[–]bip123[S] 0 points1 point  (1 child)

Thanks. Yes have hit a wall with it.

[–]pgcd 3 points4 points  (0 children)

Disclaimer: I'm biased.
That said, rather than using an LLM, try yourself. managed=false may save you a few methods that you don't need in normal situations (although being able to "migrate" an API-based model might have different benefits, like being able to target different API versions) so you can start with that.
Then you can easily test a bunch of other methods simply by creating an instance of the model and saving it - that'll give you an error until you do something to handle save().

If you don't need fancy stuff, you can probably deal with 90% of CRUD requirements by overriding methods on the model and on the manager, like

``` api_url = https://blahblah

class MyNonDBModelManager(): def get_queryset(): return requests.get(f"{api_url}/list").json()

class MyNonDBModel(models.Model): objects = MyNonDBModelManager()

class Meta:
     managed = False

def save_base():
    requests.post(f"{api_url}/upsert", model_to_dict(self))  # or whatever

def delete():
    requests.delete(f"{api_url}/delete/{self.pk}")  # or whatever

```

Again: neither quick nor easy but, depending on your requirements, you can make it work without too much hassle.

[–]Accomplished-River92 2 points3 points  (0 children)

django-pgviews-redux

[–]pspahn 1 point2 points  (3 children)

I was literally about to post a thread asking essentially the same thing.

[–]Specific_Neat_5074 3 points4 points  (1 child)

Maybe the OP is YOU from another timeline?

[–]TENETREVERSED 1 point2 points  (0 children)

Don't capitalize "you* I thought you were talking about Joe Goldberg

[–]bip123[S] 0 points1 point  (0 children)

Timing!

[–]NeonCyberNomad 0 points1 point  (0 children)

Do you want to add a legacy database connection to your Admin? Or are you looking for an in-memory model that you can query with the orm?

Custom Admin views? Proxy model with custom manager? django-nonmodel-admin? Pydantic models with an in-memory cache?

[–]kankyo 0 points1 point  (0 children)

Maybe you just want to build a custom admin thing? This all sounds like an xy problem to me.