What are the different types of Models in Loopback? - loopbackjs

Loopback framework allows you to create several types of Models. The list looks something like:
Model
PersistedModel
ACL
AccessToken
Application
...
What are these various types and when to use them?

Here's a link to the base-models
Most of our models inherit from Persisted Model. When you have an embedsMany relation, they suggest to use "Model" as your base model.
We also use User as our base model for our own website's users, only because the base model didn't have enough properties.
if you do a search for "Loopback" in GitHub, you will see a number of example repositories showing how loopback is used.

Related

Should i use proxy models in django?

im here trying to start a new project, in django
and i came across this problem, while creating the models
My users will be different types of users, so i was thinking, should i use proxy models for it?
because here is how it´s going to be the realtionship between them.
i will have a client, and a personal trainer, and the admin of course, but the problem here is that the client is going to have a ForeignKey with personal trainer, my main question is, can i create in a proxy model an extra field where i establish the relationship with personal trainer proxy model?
If not, is there any other way that i can aproach this type of situation
Thanks in advance
No. As far as I know a Proxy Model only changes the behavior of a model. You can add new methods only. If you want to add new fields it must be a Concrete Model.
You should create a new model class which inherits from AbstractUser. Then you can either add a type field which can be "client" or "personal_trainer" or "whatever". Or you could use a OneToOne field to have a UserProfile associated w/ your user - a different profile for "clients" or "personal_trainers" or "whatevers".

How to define relations in dynamically created model in loopback 4

I have a requirement where the model schema will be generated by the user from a user interface that will be exposed by REST API that means dynamic models, controllers and repositories. This already has been achieved.
Now the requirement is to allow users to specify relations between tables from the same user interface. For e.g. I am creating a table order then while defining its properties I should be able to map product_id property to product table.
How can I achieve this?
I am afraid LoopBack 4 does not offer first-class support for building model relations dynamically yet. We have been discussing this feature in GitHub issue loopback-next#2483
Essentially, there are two parts of the problem:
Create repository artifacts for navigating to related models.
This is IMO the easier part, since all you need to do is take the class constructor created by defineCrudRepositoryClass for the model you are trying to expose via REST API and create a subclass that will call methods like createHasManyRepositoryFactoryFor and registerInclusionResolver from the constructor.
Define a controller class implementing relation-specific REST API endpoints.
This is slightly more involved as you need to implement function that will define a new controller class with dynamically generated OpenAPI metadata for each model relation you want to expose via REST API. I would start with copy and paste of defineCrudRestController function and then reworking individual endpoints to provide navigational API for your relation. You should probably create multiple define{relation}Controller functions, one for each relation type (e.g. defineHasManyController).

Django model with dynamic fields based on foreign key

I am trying to implement a model where only a subset of the fields should be presented in forms, based on a foreign key in the model.
The total number of fields is relatively low (~20), but may change frequently with different values of the foreign key.
I would like to use something like single table inheritance, but with a single model (no inheritance). I looked at existing packages eav, dynamic models... but they did not seem to fit my needs.
I part of the requirement is that I would like to query the models by sql without too many joins.
Here is a use case representing applications to grants based on a request for application (rfa). The rfa is entered in the admin by staff and the applications are made by end users.
application class
rfa (request for application): foreign key
field_1
...
field_20
rfa class:
app_fields (coma separated list of field names)
In the forms, only the fields in app_fields should be visible for a particular rfa.
I have the rfa part covered with django-separatedvaluesfield and I was wondering how to implement the application pages so that they work with generic views in the frontend and admin, and be DRY.
For the update views, I could have a method in the application model, but it would not work for create as the rfa is not defined for create:
def get_current_app_fields(self):
return self.rfp.app_fields
Any ideas on the best DRY strategy to implement frontend views and admin?
Thanks.

How to write reusable apps with customizable models in Django?

I want to make reusable apps, that allow for customization by the integrator.
An example is if I make a newsletter signup app with the bare minimum of storing email address, but the integrator later wants to add additional fields, like say a name. What is the things I need to do to allow for this easily?
I went down the path of swapping out the models, like Django's auth system does, but that didn't work well. Then I found swappable attribute in the Meta class and a package that does this, but both are not intended for external use.
The only way I can think of for the integrator to do is, allow them to provide custom forms by passing it into the view in the urls for instance.
url('^someurl/$', MyView.as_view(form_class=SomeForm), name="myurl")
Then have a secondary model, with foreign keys to the internal newsletter model, but this means a secondary table that needs to be joined.
Another alternative is to try Abstract models, but I'm not sure what the impact will be there.
So what is the Django/Pythonic way of solving this?

Django add a many to one field to user object

I'm creating a Network class that has a many-to-one relationship with the Django User class. In other words, a Network can have many User and each User has only one Network. It's easy to add many-to-many relationships because I can add the many-to-many field in the Network class; however, it's hard to apply many-to-one relationship since I need to add the the foreign key in User class. There is no way I can do that.
I do have a UserProfile class that has a one-to-one relationship with User, but it's only for storing additional information about the User, not any relationships. All my other relationships defined are relating to User, not UserProfile.
Is there a way to create a many-to-one relationship to the User and Network class without using the UserProfile? Thanks!
The only things I can think of are subclassing User, which is tricky because the admin has no way to turn a base object (User in this case) into a subclassed object (NetworkUser, and yes I've tried), or creating a NetworkUser model with a OneToOne relationship to User and a ForeignKey to Network. I'd recommend the latter, because while you feel sure now that you just want to add one thing to User and it shouldn't be a big deal, later on there will be more to add.
The thing that bothers me about directly messing with User is that this is only for a single app, not necessarily the whole project, and modifying User in a way that will affect other apps in the project.