Auto-populating created\_by field with Django admin site




I want to use the Django admin interface for a very simple web application but I can't get around a problem that should not be that hard to resolve ..


Consider the following:



```
class Contact(models.Model):
    name = models.CharField(max_length=250, blank=False)
    created_by = models.ForeignKey(User, blank=False)

```

I can't find a way to auto-populate the created\_by field and make the Django admin aware of it. Most of the method I've seen implies overloading the Object's save method and pass it the request user. They all requires to build your custom views and/or forms.


Optimally the form to create new contacts *in the admin site* should not show the created\_by field (which is quite easy) and auto-populate it with the current user (which seems harder than it should).
<http://code.djangoproject.com/wiki/CookBookNewformsAdminAndUser>


Involves implementing save methods on your ModelAdmin objects.
> 
> What attributes I have to attach to the CreatedDate and ModifiedDate properties to make them automatically populated by the server based on the above scenario?
> 
> 
> 


**Solution 1)**



```
namespace Joukyuu.Models
{
    public class Passage
    {
        public int PassageId { get; set; }
        public string Contents { get; set; }


        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

       public Passage()
       {          
         this.CreatedDate  = DateTime.UtcNow;
         this.ModifiedDate = DateTime.UtcNow;
       }
    }
}

```

and by edit you have to change/update it by your self!


**Solution 2)**


Custom attribute:



```
[SqlDefaultValue(DefaultValue = "getutcdate()")]
public DateTime CreatedDate { get; set; }

```

[Entity Framework 6 Code first Default value](https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value)


**Solution 3)**


with help of Computed:



```
[Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedUtc { get; set; 


  "dbo.Products",
            c => new
                {
                    ProductId = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    CreatedUtc = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"),
                })
            .PrimaryKey(t => t.ProductId);

```

<https://andy.mehalick.com/2014/02/06/ef6-adding-a-created-datetime-column-automatically-with-code-first-migrations/>


**Solution 4)**
You can also do this with command interceptor by modifying manually the query.


**Solution 5)** 
Use Repository pattern to manage the data creation and set it by CreateNew 
This is my favour Solution!


<https://msdn.microsoft.com/en-us/library/ff649690.aspx>


**Solution 6)** 
just set it or get in in the UI or in your VM.







In **Entity Framework Core 1.0** easy:



```
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Passage>()
        .Property(b => b.CreatedDate )
        .HasDefaultValueSql("getdate()");
}

``` > 
> i want to create ap application which generates popup alerts at the day of event ocurs as well as a day before the event will occur in a specific time interval as well.. events are stored in database and they are all date fields .. so i want to link those fields to create popups .. help me .
> 
> 
> 


In order to help you, you must first provide us with what you have achieved. We do not write code for you, simply correct and enlighten on mistakes. hi phil 
congratulations on creating this. we’re about to upgrade our asana account so will be candidates for this. 
one function that would be super useful to us would be to have the subtasks of a task populate based on a selection made in a custom field. e.g. if we change content type to blog post… and it's done exactly what you told it to: generate a field that's capable of holding an SQL DATE. Which means a field without a time.  

The correct way of indicating a timestamp using JPA is as follows:



```
   /**
     * Time/date of creation of an entity.
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATION_DATE")
    protected Date creationDate;

``` [image] andrei:

a quick fix is to comment the line in the "on message" tab for the function node that is troubled

when you add a module in "setup", the "import as" name is made available to the function tabs...

[image]

... so you dont need to declare it nor do you need to set/get it from co… when you create the buton are you setting up the event handler?



```
this.EditButton.Click += new System.EventHandler(this.EditButton_OnClick);
```

Normally this created behind the scenes by visual studio. hi @checkout,if i was in your shoes. i would create a new view in your airtable for the records with completed ids. the use the airtable trigger “new record in view” select the correct view and create your action email from there.kind regards,https://www.seundebiyi.com/ The basic idea here is that your adding properties to the model that EF can use to make a relationship. Right you're trying to create a relationship of type `User` and that is creating an error. To assign a composite key each key needs to be a type compatible with a `Key` field, not a navigation property. So we add `UserId` and `FriendId` of type `int`, `string` or `GUID` etc. and create a relationship off those properties.



```
public class Relationship
{
    public User Friend { get; set; }

    public User User { get; set; }

    public int FriendId { get; set; }

    public int UserId { get; set; }

    public DateTimeOffset RelationshipInitializationDate { get; set; }
}

public class User
{
    public int UserId { get; set; }
}

```

You can now define a composite key across `UserId` and `FriendId`. Something like this should do:



```
public class NorthwindContext : DbContext
{
     public NorthwindContext(DbContextOptions<NorthwindContext> options):base(options) { }

     public NorthwindContext() { }

     protected override void OnModelCreating(ModelBuilder builder)
     {
         builder.Entity<Relationship>().HasKey(table => new {
         table.FriendId, table.UserId
         });
     }
     public DbSet<Relationship> Relationships { get; set; }
     public DbSet<User> Users { get; set; }
}

```

[Source: Medium - How To: Entity Framework Core relationships, composite keys, foreign keys, data annotations, Code First and Fluent API](https://medium.com/radzenhq/how-to-entity-framework-core-relationships-composite-keys-foreign-keys-data-annotations-code-f4b238086463) hello [@saurabhgupta](/u/saurabhgupta) :wave:

i’m afraid that the last updated column isn’t supported by our api at this time. this is because it’s calculated client side. you can see a list of these column types here - <https://monday.com/developers/v2#column-values-section-auto-calculated-column>

although the api do… so here's what we've found:

so if your form is empty and then you add content to a blank form, it actually obey's the ui:emptyvalue value

if your form is passed data when you view it, e.g you're editing data then the json schema form doesn't really understand how to utilize the ui:emptyvalue value…