models

package
v0.0.0-...-c4472bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: GPL-2.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrModelInitialized errs.Error = "model is improperly initialized"
	ErrObjectInvalid    errs.Error = "the object must be a valid pointer to a struct"
	ErrModelEmbedded    errs.Error = "the object must embed the Model struct"
	ErrModelAdressable  errs.Error = "the Model is not addressable"
)

Variables

View Source
var (
	SIGNAL_MODEL_SETUP = model_signal_pool.Get("models.Model.setup")
)

Functions

func NewSaveConfigContext

func NewSaveConfigContext(SaveConfig SaveConfig) context.Context

func SaveConfigContext

func SaveConfigContext(ctx context.Context, cnf SaveConfig) context.Context

func Setup

func Setup[T attrs.Definer](def T) T

Setup sets up a attrs.Definer object so that it's model is properly initialized.

This method is normally called automatically, but when manually defining a struct as a model, this method should be called to ensure the model is properly initialized.

In short, this must be called if the model is not created using attrs.NewObject.

Types

type BaseModelInfo

type BaseModelInfo struct {
	// contains filtered or unexported fields
}

type BaseModelProxy

type BaseModelProxy struct {
	// contains filtered or unexported fields
}

type CanControlSaving

type CanControlSaving interface {
	attrs.Definer
	ControlsEmbedderSaving() bool
}

type CanTargetDefiner

type CanTargetDefiner interface {
	attrs.Definer
	TargetContentTypeField() attrs.FieldDefinition
	TargetPrimaryField() attrs.FieldDefinition
}

type DeleteableObject

type DeleteableObject interface {
	DeleteObject(ctx context.Context) error
}

type MapDataStore

type MapDataStore map[string]interface{}

func (MapDataStore) DeleteValue

func (m MapDataStore) DeleteValue(key string) error

func (MapDataStore) GetValue

func (m MapDataStore) GetValue(key string) (any, bool)

func (MapDataStore) HasValue

func (m MapDataStore) HasValue(key string) bool

func (MapDataStore) SetValue

func (m MapDataStore) SetValue(key string, value any) error

func (MapDataStore) String

func (m MapDataStore) String() string

type Model

type Model struct {

	// ThroughModel is a model bound to the current
	// object, it will be set if the model is a
	// target of a ManyToMany or OneToMany relation
	// with a through model.
	ThroughModel attrs.Definer

	// annotations for the model, used to store
	// database annotation key value pairs
	Annotations map[string]any
	// contains filtered or unexported fields
}

The `models` package provides a Model struct that is used to represent a model in the GO-Django ORM.

To use the Model struct, simply embed it in your own attrs.Definer struct.

This will give you access to all the extra functionality provided by the `Model` struct, such as reverse relations, annotations, and the [Model.GetQuerySet] method.

To use the model directly in the code without having fetched it from the database it is recommended to use the Setup function on the attrs.Definer object, passing in the attrs.Definer object as an argument.

func ExtractModel

func ExtractModel(def attrs.Definer) (*Model, error)

func (*Model) AfterQuery

func (m *Model) AfterQuery(ctx context.Context) error

AfterQuery is called after a query is executed on the model.

This is useful for setup after the model has been loaded from the database, such as setting the initial state of the model and marking it as loaded from the database.

func (*Model) AfterSave

func (m *Model) AfterSave(ctx context.Context) error

AfterSave is called after the model is saved to the database.

This is useful for setup after the model has been saved to the database, such as setting the initial state of the model and marking it as loaded from the database.

func (*Model) Annotate

func (m *Model) Annotate(annotations map[string]any)

Annotate adds annotations to the model. Annotations are key-value pairs that can be used to store additional information about the model, such as database annotations or custom data.

func (*Model) Create

func (m *Model) Create(ctx context.Context) error

Create saves the model to the database as a new object.

It checks if the model is properly initialized and if the model's definitions are set up. If the model is not initialized, it returns an error.

func (*Model) CreateObject

func (m *Model) CreateObject(object attrs.Definer) attrs.Definer

CreateObject creates a new object of the model type and sets it up with the model's definitions.

It returns nil if the object is not valid or if the model is not registered with the model system.

This automatically sets up the model's fields and handles the proxy model if it exists.

This method is automatically called by the attrs.NewObject function when a new object is created.

func (*Model) DataStore

func (m *Model) DataStore() queries.ModelDataStore

DataStore returns the data store for the model.

The data store is used to store model data like annotations, custom data, etc. If the data store is not initialized, it will be created.

func (*Model) Define

func (m *Model) Define(def attrs.Definer, flds ...any) *attrs.ObjectDefinitions

Define defines the fields of the model based on the provided definer

Normally this would be done with attrs.Define, the current model method is a convenience method which also handles the setup of the model as well as reverse relation setup.

func (*Model) Defs

func (m *Model) Defs() *attrs.ObjectDefinitions

Defs returns the model's definitions.

If the model is not properly initialized it will panic.

func (*Model) Delete

func (m *Model) Delete(ctx context.Context) error

Delete deletes the model from the database.

func (*Model) DeleteObject

func (m *Model) DeleteObject(ctx context.Context) error

func (*Model) ModelMeta

func (m *Model) ModelMeta() attrs.ModelMeta

ModelMeta returns the model's metadata.

func (*Model) Object

func (m *Model) Object() attrs.Definer

Object returns the object of the model.

It checks if the model is properly initialized and if the object is set up, if the model is not properly set up, it will panic.

func (*Model) PK

func (m *Model) PK() attrs.Field

PK returns the primary key field of the model.

If the model is not properly initialized it will panic.

If the model does not have a primary key defined, it will return nil.

func (*Model) Save

func (m *Model) Save(ctx context.Context) error

Save saves the model to the database.

It checks if the model is properly initialized and if the model's definitions are set up. If the model is not initialized, it returns an error.

If the model is initialized, it calls the SaveObject method on the model's object, passing the current context and a SaveConfig struct that contains the model's object, query set, fields to save, and a force flag.

The object embedding the model can choose to implement the [canSaveObject] interface to provide a custom save implementation.

func (*Model) SaveObject

func (m *Model) SaveObject(ctx context.Context, cnf SaveConfig) (err error)

SaveObject saves the model's object to the database.

It checks if the model is properly initialized and if the model's definitions are set up. If the model is not initialized, it returns an error.

If the model is initialized, it iterates over the model's fields and checks if any of the fields have changed. If any field has changed, it adds the field to the list of changed fields and prepares a queryset to save the model.

A config struct SaveConfig is used to pass the model's object, queryset, fields to save, and a force flag to indicate whether to force the save operation.

func (*Model) Saved

func (m *Model) Saved() bool

Saved checks if the model is saved to the database. It checks if the model is properly initialized and if the model's definitions are set up. If the model is not initialized, it returns false. If the model is initialized, it checks if the model was loaded from the database or if the primary key field is set. If the primary key field is nil, it returns false. If the primary key field has a value, it returns true.

func (*Model) SetThroughModel

func (m *Model) SetThroughModel(throughModel attrs.Definer)

If this model was the target end of a through relation, this method will set the through model for this model.

func (*Model) Setup

func (m *Model) Setup(def attrs.Definer) error

func (*Model) SignalChange

func (m *Model) SignalChange(fa attrs.Field, value interface{})

SignalChanged sends a signal that the model has changed.

This is used to allow the attrs.Definitions to callback to the model and notify it that the model has changed, so it can update its internal state and trigger any necessary updates.

func (*Model) SignalReset

func (m *Model) SignalReset(fa attrs.Field)

SignalReset is called when a field's changed status should be reset.

func (*Model) State

func (m *Model) State() *ModelState

State returns the current state of the model.

The state is initialized when the model is setup, and it contains the initial values of the model's fields as well as the changed fields.

func (*Model) Update

func (m *Model) Update(ctx context.Context) error

Update saves the model to the database as an update operation.

It checks if the model is properly initialized and if the model's definitions are set up. If the model is not initialized, it returns an error.

func (*Model) Validate

func (m *Model) Validate(ctx context.Context) error

Validate checks if the model is valid.

type ModelChangeSignal

type ModelChangeSignal struct {
	Next        *ModelChangeSignal
	Flags       ModelSignalFlag
	Model       *Model
	Object      attrs.Definer
	Field       attrs.Field
	StructField *reflect.StructField
}

type ModelSignal

type ModelSignal struct {
	SignalInfo ModelSignalInfo
	Model      *Model
	Object     attrs.Definer
}

type ModelSignalFlag

type ModelSignalFlag uint
const (
	ModelSignalFlagNone ModelSignalFlag = 0
	FlagModelReset      ModelSignalFlag = 1 << iota
	FlagModelSetup
	FlagProxySetup
	FlagProxyChanged
	FlagFieldChanged
	FlagFieldReset
)

func (ModelSignalFlag) True

func (f ModelSignalFlag) True(flag ModelSignalFlag) bool

type ModelSignalInfo

type ModelSignalInfo struct {
	Flags ModelSignalFlag
	Data  map[string]any
}

type ModelState

type ModelState struct {
	// contains filtered or unexported fields
}

ModelState represents the state of a model instance, tracking which fields have been changed since the last reset.

A nil *ModelState is considered a valid state, meaning that the model has no state to track, the model is considered new and fully 'changed'.

func (*ModelState) Changed

func (m *ModelState) Changed(checkState bool) bool

Changed returns true if the model's state has changed, meaning that at least one field has been modified since the last time the state was checked or reset.

If checkState is true, it will first check the state to ensure that the changed fields are up to date.

func (*ModelState) HasChanged

func (m *ModelState) HasChanged(fieldName string) bool

HasChanged checks if a specific field has been changed in the model's state.

func (*ModelState) InitialValue

func (m *ModelState) InitialValue(fieldName string) (interface{}, bool)

InitialValue returns the initial value of a field in the model's state.

func (*ModelState) Mark

func (m *ModelState) Mark(fieldName string)

Mark a field as changed in the model's state. It will also check the state to ensure that the changed fields are up to date before marking the field as changed.

func (*ModelState) Reset

func (m *ModelState) Reset()

Reset clears the changed fields and initial values and reinitializes the initial values from the model's definitions. This is useful when the model is saved or reset.

type ProxyFieldConfig

type ProxyFieldConfig struct {
	Proxy            attrs.Definer
	ContentTypeField string
	TargetField      string
	AllowEdit        bool
}

type SaveConfig

type SaveConfig struct {

	// A custom queryset to use for creating or updating the model.
	QuerySet *queries.QuerySet[attrs.Definer]

	// Fields to save, if empty, all fields will be saved.
	// If the model is not loaded from the database, all fields will be saved.
	Fields []string

	// IncludeFields are fields which must be saved, even if they have not changed
	// according to the model's state.
	// This is used to force the save operation to include fields
	// that are not changed, but must be saved for some reason.
	IncludeFields []string

	ForceCreate bool
	ForceUpdate bool
	// contains filtered or unexported fields
}

func (SaveConfig) Force

func (cnf SaveConfig) Force() bool

type SaveableObject

type SaveableObject interface {
	SaveObject(ctx context.Context, cnf SaveConfig) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL