Skip to main content

InlineFormSet

Struct InlineFormSet 

Source
pub struct InlineFormSet<P: FormModel, C: FormModel> { /* private fields */ }
Expand description

InlineFormSet for managing forms related to a parent model

InlineFormSets are used to edit related objects together with a parent object, similar to Django’s inline formsets for admin.

Implementations§

Source§

impl<P: FormModel, C: FormModel> InlineFormSet<P, C>

Source

pub fn new(parent: P, fk_field: String) -> Self

Create a new InlineFormSet

§Arguments
  • parent - The parent model instance
  • fk_field - The foreign key field name on the child model
§Examples
let parent = Author { id: 1, name: "John".to_string() };
let formset = InlineFormSet::new(parent, "author_id".to_string());
Source

pub fn add_child_form(&mut self, form: ModelForm<C>)

Add a child form to the formset

Source

pub fn parent(&self) -> &P

Get the parent model instance

Source

pub fn fk_field(&self) -> &str

Get the foreign key field name

Source

pub fn child_forms(&self) -> &[ModelForm<C>]

Get all child forms

Source

pub fn save(&mut self) -> Result<(), FormError>

Save the formset and all related child instances.

This method saves the parent model first, retrieves the parent’s primary key, sets the foreign key on each child instance, then saves each child.

§Errors

Returns an error if any save operation fails or if the parent model does not have an id field after saving.

Source

pub fn is_valid(&mut self) -> bool

Validate all child forms

Auto Trait Implementations§

§

impl<P, C> Freeze for InlineFormSet<P, C>
where P: Freeze,

§

impl<P, C> !RefUnwindSafe for InlineFormSet<P, C>

§

impl<P, C> Send for InlineFormSet<P, C>

§

impl<P, C> Sync for InlineFormSet<P, C>

§

impl<P, C> Unpin for InlineFormSet<P, C>
where P: Unpin, C: Unpin,

§

impl<P, C> UnsafeUnpin for InlineFormSet<P, C>
where P: UnsafeUnpin,

§

impl<P, C> !UnwindSafe for InlineFormSet<P, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.