model-bakers/model_bakery

Need option to skip fields

PritamDutt opened this issue · 2 comments

Currently there is no option to skip certain fields when calling baker.make or I could not find if there is.

My use case:

We have certain unmanaged Django models which are mapped to external reporting tables beyond the scope of application, and lack primary key.

We are currently using ctid [system column] as the primary key when mapping them in Django.

Now in order to run some tests, we want to be able to insert data in them, without generating data for ctid field.

Currently this is turning out to be challenge using model_bakery

Versions

Python: 3.8
Django: 3.2
Model Bakery: 1.3.2

Hi @PritamDutt, thanks for opening this issue.

Unfortunately there's no way in the current API to skip fields. The idea behind model bakery is to mirror everything that's being defined by Django models, such as not creating values for nullable fields or using default values when they exist. Also, model bakery only supports Django's default model fields and this can be causing issues for you too. I don't thing Django supports system columns by default and, thus, neither model-bakery.

I think adding the ability to ignore other fields that aren't defined as optional or nullable in the models can make the internal API for model bakery more complex and it's possible that there are ways to avoid this issue with the lib. Also such API can potentially violate Django's default behavior and lead to more errors. For example, what would happen if a user tries to skip a non-nullable field? Should bakery allow it? If not, how to notify the error? In my opinion, such questions, despite of increasing the lib's complexity, can potentially frustrate users because the API will suggest they can do something (skip fields) that, in reality, they shouldn't (db constraints doesn't allow it). Even though it can make sense for your specific usage of ctid, I don't think this is a feature model-bakery is missing. =/

Anyway, I'm not familiar with ctid usage in Django. But, my suggestions for similar cases are:

  1. Add a default value or make these columns nullables (although I can imagine that this is not ideal);
  2. Use Recipes instead of directly calling baker.make for such models. You can then define a sequence for that column so you'll have more control on how to populate it;
  3. Implement a custom generator function for your field type and configure baker to use it instead of relying on the default;

Happy baking =)

Thank you @berinhard for letting me know.. since ctid is a system column, and primary key cannot be null.. I will look out for more options in my use case..

Thank you once again for quick response.