#35269: GeneratedFields can't be defined on RelatedFields
-------------------------------------+-------------------------------------
     Reporter:  Perrine L.           |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 You don't need any `GeneratedField` subclass to achieve what you're after,
 simply use `ForeignObject` which is the accessor base of `ForeignKey`.

 {{{#!python
 from django.db.models.fields.related_fields import ForeignObject

 class Event(models.Model):
     last_updated_by_id = models.GeneratedField(
         expression=Greatest("created_by", "confirmed_by", "canceled_by"),
         output_field=models.BigIntegerField(),
         db_index=True,
         db_persist=True,
     )
     last_updated_by = ForeignObject(
         User,
         models.SET_NULL,
         from_fields=["last_updated_by_id"],
         to_fields=["id"],
         related_name="last_updated_events_set",
         null=True,
     )
 }}}

 I also believe there is no reason to add this feature in core as with
 these two primitives it should be trivial to implement a
 `ForeignGeneratedField(ForeignObject)` that does exactly what you're after
 in a third-party application for the rare use case this is useful.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35269#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018ea417fba1-839a1362-c265-4d37-8378-d41e507e294c-000000%40eu-central-1.amazonses.com.

Reply via email to