nautobot / nautobot

Network Source of Truth & Network Automation Platform
https://docs.nautobot.com
Apache License 2.0
1.07k stars 283 forks source link

Support content_types.set by field name, not requiring ID #5028

Open jvanderaa opened 10 months ago

jvanderaa commented 10 months ago

As ...

Austin - Network Automation Engineer

I want ...

to have Nautobot determine the ID for me when I use the set or add methods, not require me to have to figure out the ID first.

So that ...

Development makes sense. I understand the Django side of things and working with IDs. But from a network automation app development perspective, now every developer that wants to add content types has to figure out the code to first get the ID from the name of the content type. That is not very DRY when it should just be in Nautobot.

This code should in my opinion be valid:

location_type.content_types.set(["dcim.device"])

I know this is done when...

When I can execute location_type.content_types.set(["dcim.device"])

Optional - Feature groups this request pertains to.

Database Changes

None

External Dependencies

No response

glennmatthews commented 10 months ago

This might be doable, but it would require creating a custom subclass of ManyToManyField that understands setting by name instead of setting by value, and updating models to use that subclass.

(edit) Also you don't need the ID per se - location_type.content_types.add(ContentType.objects.get_for_model(Device)) should work just fine.