Skip to content

Conversation

@timgraham
Copy link
Collaborator

@timgraham timgraham commented Apr 21, 2025

Querying support will be added later (#303).

fixes #219

@timgraham timgraham changed the title Add EmbeddedModelArrayField INTPYTHON-483 Add EmbeddedModelArrayField May 5, 2025
@WaVEV WaVEV force-pushed the arrayfield+emf branch from 4f91ba9 to f109cf5 Compare May 5, 2025 02:16
@timgraham timgraham force-pushed the arrayfield+emf branch 2 times, most recently from bce0a46 to 825ffca Compare May 23, 2025 13:21
@timgraham timgraham marked this pull request as ready for review May 23, 2025 14:11
@timgraham timgraham requested review from Jibola and WaVEV May 23, 2025 14:11
Copy link
Collaborator

@WaVEV WaVEV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only few details to clarify.

Jibola
Jibola previously requested changes May 29, 2025
@timgraham timgraham force-pushed the arrayfield+emf branch 2 times, most recently from d93ee38 to f528213 Compare June 2, 2025 18:54
@timgraham timgraham dismissed Jibola’s stale review June 2, 2025 18:55

support for nested fields added

@timgraham timgraham merged commit fe24e7a into mongodb:main Jun 2, 2025
15 checks passed
@timgraham timgraham deleted the arrayfield+emf branch July 23, 2025 18:38
@aclark4life
Copy link
Collaborator

aclark4life commented Oct 31, 2025

@timgraham If Company embeds Project and Project embeds Invoice and embeds Time, can I set extra_forms on any of the nested models below Project ?

class CompanyAdminForm(forms.ModelForm):                                        
    class Meta:                                                                 
        model = Company                                                         
        fields = "__all__"                                                      
        widgets = {                                                             
            "website": forms.URLInput(attrs={"size": 60}),                      
        }                                                                       
    projects = EmbeddedModelArrayField(Project,                                                                                                             
        prefix="project", extra_forms=0,                                        
    )                                             

Models

from django.db import models
from django_mongodb_backend.fields import EmbeddedModelArrayField
from django_mongodb_backend.models import EmbeddedModel


class Company(models.Model):
    name = models.CharField(max_length=255)
    address = models.TextField()
    website = models.URLField()
    projects = EmbeddedModelArrayField("Project")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Companies"


class Time(EmbeddedModel):
    date = models.DateField(null=True, blank=True)
    hours = models.DecimalField(max_digits=5, decimal_places=2)
    description = models.TextField(blank=True)

    def __str__(self):
        return f"{self.hours}h on {self.date}"


class Invoice(EmbeddedModel):
    number = models.CharField(max_length=50)
    date = models.DateField(null=True, blank=True)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    times = EmbeddedModelArrayField("Time")  # embedded time entries

    def __str__(self):
        return f"Invoice {self.number}"


class Project(EmbeddedModel):
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    start_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)
    invoices = EmbeddedModelArrayField("Invoice")  # embedded invoices

    def __str__(self):
        return self.name

@timgraham
Copy link
Collaborator Author

I don't think so. The implementation of embedded forms is very limited,
p.s. Please don't set a precedent of using pull request comments as a support channel.

@aclark4life
Copy link
Collaborator

I don't think so. The implementation of embedded forms is very limited,

How do I start thinking about supporting this, assuming it's possible and practical to do so ?

p.s. Please don't set a precedent of using pull request comments as a support channel.

I wasn't tracking this feature when the PR got merged, so let's consider this a follow up code review rather than a support request. Based on your response, and previous discussions about polishing the forms, I'm slightly more interested in doing that work now since it appears to be a common use case for nested embeds.

@timgraham
Copy link
Collaborator Author

I don't know, offhand. The implementation is a kludge because Django forms and formsets were never meant to be nested. It may be necessary to rethink the implementation entirely and introduce some new APIs that are better designed for the requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add EmbeddedModelField support to ArrayField

4 participants