Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
devkral committed Jan 12, 2025
1 parent 88063df commit c46846f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
14 changes: 13 additions & 1 deletion docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ For this the `StrictModel` model can be used. Otherwise it behaves like a normal

There is no strict version of a `ReflectModel` because the laxness is required.


### Loading models

You may have the models distributed among multiple files and packages.
Expand Down Expand Up @@ -97,6 +98,17 @@ If no `id` is declared in the model, **Edgy** will automatically generate an `id

Earlier there were many restrictions. Now they were lifted

### Controlling collision behaviour

Earlier models were simply replaced when defining a model with the same name or adding such.

Now the default is to error when a collision was detected, or in case the `on_conflict` parameter was set, either
a `replace` or `keep` executed.

``` python
{!> ../docs_src/models/on_conflict.py !}
```

#### What you should not do

##### Declaring an IntegerField as primary key without autoincrement set
Expand Down Expand Up @@ -171,7 +183,7 @@ to copy a model class and optionally add it to an other registry.

You can add it to a registry later by using:

`model_class.add_to_registry(registry, name="", database=None, replace_related_field=False)`
`model_class.add_to_registry(registry, name="", database=None, replace_related_field=...)`

In fact the last method is called when the registry parameter of `copy_edgy_model` is not `None`.

Expand Down
4 changes: 2 additions & 2 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ hide:
### Added

- ManyToManyField `create_through_model` method allows now the keyword only argument `replace_related_field`.
- `add_to_registry` has now an additional keyword-only argument `on_conflict` for controlling what happens when a same named model already exists.
- `add_to_registry` and models have now an additional keyword-only argument `on_conflict` for controlling what happens when a same named model already exists.
For models this can be passed : `class Foo(edgy.Model, on_conflict="keep"): ...`.
- Passing a tuple or list of types to `replace_related_field` is now allowed.
- Add `through_registry` to ManyToMany.
- Add `no_copy` to MetaInfo.
- Add `ModelCollisionError` exception.

### Changed

- `add_to_registry` deprecates passing arguments as positional arguments except the first one (registry).
- `create_edgy_model` passes through additional keyword arguments to the edgy model class.
- RelatedField uses now `no_copy`.
- Through models use now `no_copy` when autogenerated.
Expand Down
19 changes: 19 additions & 0 deletions docs_src/models/on_conflict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import edgy

models = ...


class Foo(edgy.Model, on_conflict="keep"):
class Meta:
registry = models


# or


class Foo2(edgy.Model):
class Meta:
registry = False


Foo2.add_to_registry(models, name="Foo", on_conflict="replace")
3 changes: 2 additions & 1 deletion edgy/core/db/models/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def __new__(
attrs: dict[str, Any],
meta_info_class: type[MetaInfo] = MetaInfo,
skip_registry: bool = False,
on_conflict: Literal["error", "replace", "keep"] = "error",
**kwargs: Any,
) -> Any:
fields: dict[str, BaseFieldType] = {}
Expand Down Expand Up @@ -793,7 +794,7 @@ def __new__(
if not meta.registry:
new_class.model_rebuild(force=True)
return new_class
new_class.add_to_registry(meta.registry, database=database)
new_class.add_to_registry(meta.registry, database=database, on_conflict=on_conflict)
return new_class

def get_db_schema(cls) -> Union[str, None]:
Expand Down

0 comments on commit c46846f

Please sign in to comment.